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_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
91 static int wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
);
94 static void wpas_p2p_scan_res_handler(struct wpa_supplicant
*wpa_s
,
95 struct wpa_scan_results
*scan_res
)
99 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
102 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS)",
103 (int) scan_res
->num
);
105 for (i
= 0; i
< scan_res
->num
; i
++) {
106 struct wpa_scan_res
*bss
= scan_res
->res
[i
];
107 if (p2p_scan_res_handler(wpa_s
->global
->p2p
, bss
->bssid
,
108 bss
->freq
, bss
->age
, bss
->level
,
109 (const u8
*) (bss
+ 1),
114 p2p_scan_res_handled(wpa_s
->global
->p2p
);
118 static int wpas_p2p_scan(void *ctx
, enum p2p_scan_type type
, int freq
,
119 unsigned int num_req_dev_types
,
120 const u8
*req_dev_types
, const u8
*dev_id
, u16 pw_id
)
122 struct wpa_supplicant
*wpa_s
= ctx
;
123 struct wpa_supplicant
*ifs
;
124 struct wpa_driver_scan_params params
;
126 struct wpabuf
*wps_ie
, *ies
;
127 int social_channels
[] = { 2412, 2437, 2462, 0, 0 };
130 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
133 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
134 if (ifs
->sta_scan_pending
&&
135 wpas_p2p_in_progress(wpa_s
) == 2) {
136 wpa_printf(MSG_DEBUG
, "Delaying P2P scan to allow "
137 "pending station mode scan to be "
138 "completed on interface %s", ifs
->ifname
);
139 wpa_s
->global
->p2p_cb_on_scan_complete
= 1;
140 wpa_supplicant_req_scan(ifs
, 0, 0);
145 os_memset(¶ms
, 0, sizeof(params
));
147 /* P2P Wildcard SSID */
148 params
.num_ssids
= 1;
149 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
150 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
152 wpa_s
->wps
->dev
.p2p
= 1;
153 wps_ie
= wps_build_probe_req_ie(pw_id
, &wpa_s
->wps
->dev
,
154 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
,
155 num_req_dev_types
, req_dev_types
);
159 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
160 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
165 wpabuf_put_buf(ies
, wps_ie
);
168 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, dev_id
);
170 params
.p2p_probe
= 1;
171 params
.extra_ies
= wpabuf_head(ies
);
172 params
.extra_ies_len
= wpabuf_len(ies
);
175 case P2P_SCAN_SOCIAL
:
176 params
.freqs
= social_channels
;
180 case P2P_SCAN_SOCIAL_PLUS_ONE
:
181 social_channels
[3] = freq
;
182 params
.freqs
= social_channels
;
186 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
191 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
193 ifs
->scan_res_handler
== wpas_p2p_scan_res_handler
) {
194 wpa_s
->global
->p2p_cb_on_scan_complete
= 1;
200 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
206 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
208 switch (p2p_group_interface
) {
209 case P2P_GROUP_INTERFACE_PENDING
:
210 return WPA_IF_P2P_GROUP
;
211 case P2P_GROUP_INTERFACE_GO
:
212 return WPA_IF_P2P_GO
;
213 case P2P_GROUP_INTERFACE_CLIENT
:
214 return WPA_IF_P2P_CLIENT
;
217 return WPA_IF_P2P_GROUP
;
221 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
223 size_t ssid_len
, int *go
)
227 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
228 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
229 if (s
->disabled
!= 0 || !s
->p2p_group
||
230 s
->ssid_len
!= ssid_len
||
231 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
233 if (s
->mode
== WPAS_MODE_P2P_GO
&&
234 s
!= wpa_s
->current_ssid
)
237 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
246 static int wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
,
247 enum p2p_group_removal_reason removal_reason
)
249 struct wpa_ssid
*ssid
;
253 ssid
= wpa_s
->current_ssid
;
256 * The current SSID was not known, but there may still be a
257 * pending P2P group interface waiting for provisioning or a
258 * P2P group that is trying to reconnect.
260 ssid
= wpa_s
->conf
->ssid
;
262 if (ssid
->p2p_group
&& ssid
->disabled
!= 2)
267 wpa_s
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)
269 wpa_printf(MSG_ERROR
, "P2P: P2P group interface "
274 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
276 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
277 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
278 wpa_s
->reassociate
= 0;
279 wpa_s
->disconnected
= 1;
280 wpa_supplicant_deauthenticate(wpa_s
,
281 WLAN_REASON_DEAUTH_LEAVING
);
285 if (wpa_s
->cross_connect_in_use
) {
286 wpa_s
->cross_connect_in_use
= 0;
287 wpa_msg(wpa_s
->parent
, MSG_INFO
,
288 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
289 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
291 switch (removal_reason
) {
292 case P2P_GROUP_REMOVAL_REQUESTED
:
293 reason
= " reason=REQUESTED";
295 case P2P_GROUP_REMOVAL_FORMATION_FAILED
:
296 reason
= " reason=FORMATION_FAILED";
298 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
299 reason
= " reason=IDLE";
301 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
302 reason
= " reason=UNAVAILABLE";
304 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION
:
305 reason
= " reason=GO_ENDING_SESSION";
311 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
) {
312 wpa_msg(wpa_s
->parent
, MSG_INFO
,
313 P2P_EVENT_GROUP_REMOVED
"%s %s%s",
314 wpa_s
->ifname
, gtype
, reason
);
317 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
318 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
320 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
&& ssid
)
321 wpas_notify_p2p_group_removed(wpa_s
, ssid
, gtype
);
323 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
324 struct wpa_global
*global
;
326 enum wpa_driver_if_type type
;
327 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
329 global
= wpa_s
->global
;
330 ifname
= os_strdup(wpa_s
->ifname
);
331 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
332 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
, 0);
333 wpa_s
= global
->ifaces
;
335 wpa_drv_if_remove(wpa_s
, type
, ifname
);
340 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
341 if (ssid
&& (ssid
->p2p_group
||
342 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
343 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
345 if (ssid
== wpa_s
->current_ssid
) {
346 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
347 eapol_sm_notify_config(wpa_s
->eapol
, NULL
, NULL
);
348 wpa_s
->current_ssid
= NULL
;
351 * Networks objects created during any P2P activities are not
352 * exposed out as they might/will confuse certain non-P2P aware
353 * applications since these network objects won't behave like
356 * Likewise, we don't send out network removed signals for such
359 wpa_config_remove_network(wpa_s
->conf
, id
);
360 wpa_supplicant_clear_status(wpa_s
);
361 wpa_supplicant_cancel_sched_scan(wpa_s
);
362 wpa_s
->sta_scan_pending
= 0;
364 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
368 wpa_supplicant_ap_deinit(wpa_s
);
370 wpa_drv_deinit_p2p_cli(wpa_s
);
376 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
378 const u8
*ssid
, size_t ssid_len
)
386 if (wpa_s
->go_params
)
387 bssid
= wpa_s
->go_params
->peer_interface_addr
;
389 bssid
= wpa_s
->bssid
;
391 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
393 u8 iface_addr
[ETH_ALEN
];
394 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
396 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
399 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
400 "group is persistent - BSS " MACSTR
" not found",
405 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
407 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
408 "group is persistent - BSS " MACSTR
409 " did not include P2P IE", MAC2STR(bssid
));
410 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
411 (u8
*) (bss
+ 1), bss
->ie_len
);
412 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
413 ((u8
*) bss
+ 1) + bss
->ie_len
,
418 group_capab
= p2p_get_group_capab(p2p
);
419 addr
= p2p_get_go_dev_addr(p2p
);
420 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
421 "group_capab=0x%x", group_capab
);
423 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
424 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
427 os_memset(go_dev_addr
, 0, ETH_ALEN
);
430 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
431 "go_dev_addr=" MACSTR
,
432 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
434 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
438 static int wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
439 struct wpa_ssid
*ssid
,
440 const u8
*go_dev_addr
)
445 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
446 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
447 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
448 if (s
->disabled
== 2 &&
449 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
450 s
->ssid_len
== ssid
->ssid_len
&&
451 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
456 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
458 if (ssid
->passphrase
&& !s
->passphrase
)
460 else if (ssid
->passphrase
&& s
->passphrase
&&
461 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
464 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
467 s
= wpa_config_add_network(wpa_s
->conf
);
472 * Instead of network_added we emit persistent_group_added
473 * notification. Also to keep the defense checks in
474 * persistent_group obj registration method, we set the
475 * relevant flags in s to designate it as a persistent group.
478 s
->p2p_persistent_group
= 1;
479 wpas_notify_persistent_group_added(wpa_s
, s
);
480 wpa_config_set_network_defaults(s
);
484 s
->p2p_persistent_group
= 1;
487 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
488 s
->mode
= ssid
->mode
;
489 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
490 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
491 s
->proto
= WPA_PROTO_RSN
;
492 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
494 if (ssid
->passphrase
) {
495 os_free(s
->passphrase
);
496 s
->passphrase
= os_strdup(ssid
->passphrase
);
500 os_memcpy(s
->psk
, ssid
->psk
, 32);
502 if (s
->passphrase
&& !s
->psk_set
)
503 wpa_config_update_psk(s
);
504 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
506 s
->ssid
= os_malloc(ssid
->ssid_len
);
509 s
->ssid_len
= ssid
->ssid_len
;
510 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
513 #ifndef CONFIG_NO_CONFIG_WRITE
514 if (changed
&& wpa_s
->conf
->update_config
&&
515 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
516 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
518 #endif /* CONFIG_NO_CONFIG_WRITE */
524 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant
*wpa_s
,
527 struct wpa_ssid
*ssid
, *s
;
532 ssid
= wpa_s
->current_ssid
;
533 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
534 !ssid
->p2p_persistent_group
)
537 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
538 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
541 if (s
->ssid_len
== ssid
->ssid_len
&&
542 os_memcmp(s
->ssid
, ssid
->ssid
, s
->ssid_len
) == 0)
549 for (i
= 0; s
->p2p_client_list
&& i
< s
->num_p2p_clients
; i
++) {
550 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
, addr
,
554 if (i
== s
->num_p2p_clients
- 1)
555 return; /* already the most recent entry */
557 /* move the entry to mark it most recent */
558 os_memmove(s
->p2p_client_list
+ i
* ETH_ALEN
,
559 s
->p2p_client_list
+ (i
+ 1) * ETH_ALEN
,
560 (s
->num_p2p_clients
- i
- 1) * ETH_ALEN
);
561 os_memcpy(s
->p2p_client_list
+
562 (s
->num_p2p_clients
- 1) * ETH_ALEN
, addr
, ETH_ALEN
);
567 if (!found
&& s
->num_p2p_clients
< P2P_MAX_STORED_CLIENTS
) {
568 n
= os_realloc_array(s
->p2p_client_list
,
569 s
->num_p2p_clients
+ 1, ETH_ALEN
);
572 os_memcpy(n
+ s
->num_p2p_clients
* ETH_ALEN
, addr
, ETH_ALEN
);
573 s
->p2p_client_list
= n
;
574 s
->num_p2p_clients
++;
576 /* Not enough room for an additional entry - drop the oldest
578 os_memmove(s
->p2p_client_list
,
579 s
->p2p_client_list
+ ETH_ALEN
,
580 (s
->num_p2p_clients
- 1) * ETH_ALEN
);
581 os_memcpy(s
->p2p_client_list
+
582 (s
->num_p2p_clients
- 1) * ETH_ALEN
,
586 #ifndef CONFIG_NO_CONFIG_WRITE
587 if (wpa_s
->parent
->conf
->update_config
&&
588 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
589 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
590 #endif /* CONFIG_NO_CONFIG_WRITE */
594 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
597 struct wpa_ssid
*ssid
;
598 const char *ssid_txt
;
601 u8 go_dev_addr
[ETH_ALEN
];
605 * This callback is likely called for the main interface. Update wpa_s
606 * to use the group interface if a new interface was created for the
609 if (wpa_s
->global
->p2p_group_formation
)
610 wpa_s
= wpa_s
->global
->p2p_group_formation
;
611 wpa_s
->global
->p2p_group_formation
= NULL
;
612 wpa_s
->p2p_in_provisioning
= 0;
615 wpa_msg(wpa_s
->parent
, MSG_INFO
,
616 P2P_EVENT_GROUP_FORMATION_FAILURE
);
617 wpas_p2p_group_delete(wpa_s
,
618 P2P_GROUP_REMOVAL_FORMATION_FAILED
);
622 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_FORMATION_SUCCESS
);
624 ssid
= wpa_s
->current_ssid
;
625 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
626 ssid
->mode
= WPAS_MODE_P2P_GO
;
627 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
628 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
633 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
634 client
= ssid
->mode
== WPAS_MODE_INFRA
;
635 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
636 persistent
= ssid
->p2p_persistent_group
;
637 os_memcpy(go_dev_addr
, wpa_s
->global
->p2p_dev_addr
,
640 persistent
= wpas_p2p_persistent_group(wpa_s
,
646 client
= wpa_s
->p2p_group_interface
==
647 P2P_GROUP_INTERFACE_CLIENT
;
648 os_memset(go_dev_addr
, 0, ETH_ALEN
);
651 wpa_s
->show_group_started
= 0;
654 * Indicate event only after successfully completed 4-way
655 * handshake, i.e., when the interface is ready for data
658 wpa_s
->show_group_started
= 1;
659 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
661 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
662 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
663 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
665 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
666 MAC2STR(go_dev_addr
),
667 persistent
? " [PERSISTENT]" : "");
668 wpas_p2p_cross_connect_setup(wpa_s
);
669 wpas_p2p_set_group_idle_timeout(wpa_s
);
671 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
672 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
673 "go_dev_addr=" MACSTR
"%s",
674 wpa_s
->ifname
, ssid_txt
, ssid
? ssid
->frequency
: 0,
675 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
676 MAC2STR(go_dev_addr
),
677 persistent
? " [PERSISTENT]" : "");
678 wpas_p2p_cross_connect_setup(wpa_s
);
679 wpas_p2p_set_group_idle_timeout(wpa_s
);
683 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
685 if (network_id
< 0 && ssid
)
686 network_id
= ssid
->id
;
688 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
692 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant
*wpa_s
,
694 const u8
*dst
, const u8
*src
,
696 const u8
*data
, size_t data_len
,
697 enum offchannel_send_action_result
700 enum p2p_send_action_result res
= P2P_SEND_ACTION_SUCCESS
;
702 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
704 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
708 case OFFCHANNEL_SEND_ACTION_SUCCESS
:
709 res
= P2P_SEND_ACTION_SUCCESS
;
711 case OFFCHANNEL_SEND_ACTION_NO_ACK
:
712 res
= P2P_SEND_ACTION_NO_ACK
;
714 case OFFCHANNEL_SEND_ACTION_FAILED
:
715 res
= P2P_SEND_ACTION_FAILED
;
719 p2p_send_action_cb(wpa_s
->global
->p2p
, freq
, dst
, src
, bssid
, res
);
721 if (result
!= OFFCHANNEL_SEND_ACTION_SUCCESS
&&
722 wpa_s
->pending_pd_before_join
&&
723 (os_memcmp(dst
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
724 os_memcmp(dst
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0) &&
725 wpa_s
->p2p_fallback_to_go_neg
) {
726 wpa_s
->pending_pd_before_join
= 0;
727 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No ACK for PD Req "
728 "during p2p_connect-auto");
729 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
735 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
736 const u8
*src
, const u8
*bssid
, const u8
*buf
,
737 size_t len
, unsigned int wait_time
)
739 struct wpa_supplicant
*wpa_s
= ctx
;
740 return offchannel_send_action(wpa_s
, freq
, dst
, src
, bssid
, buf
, len
,
742 wpas_p2p_send_action_tx_status
, 1);
746 static void wpas_send_action_done(void *ctx
)
748 struct wpa_supplicant
*wpa_s
= ctx
;
749 offchannel_send_action_done(wpa_s
);
753 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
754 struct p2p_go_neg_results
*params
)
756 if (wpa_s
->go_params
== NULL
) {
757 wpa_s
->go_params
= os_malloc(sizeof(*params
));
758 if (wpa_s
->go_params
== NULL
)
761 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
766 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
767 struct p2p_go_neg_results
*res
)
769 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
770 MAC2STR(res
->peer_interface_addr
));
771 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
772 res
->ssid
, res
->ssid_len
);
773 wpa_supplicant_ap_deinit(wpa_s
);
774 wpas_copy_go_neg_results(wpa_s
, res
);
775 if (res
->wps_method
== WPS_PBC
)
776 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
778 u16 dev_pw_id
= DEV_PW_DEFAULT
;
779 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
780 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
781 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
782 wpa_s
->p2p_pin
, 1, dev_pw_id
);
787 static void p2p_go_configured(void *ctx
, void *data
)
789 struct wpa_supplicant
*wpa_s
= ctx
;
790 struct p2p_go_neg_results
*params
= data
;
791 struct wpa_ssid
*ssid
;
794 ssid
= wpa_s
->current_ssid
;
795 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
796 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
797 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
798 wpa_s
->global
->p2p_group_formation
= NULL
;
799 if (os_strlen(params
->passphrase
) > 0) {
800 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
801 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
802 "go_dev_addr=" MACSTR
"%s", wpa_s
->ifname
,
803 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
804 ssid
->frequency
, params
->passphrase
,
805 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
806 params
->persistent_group
? " [PERSISTENT]" :
810 wpa_snprintf_hex(psk
, sizeof(psk
), params
->psk
,
811 sizeof(params
->psk
));
812 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
813 "%s GO ssid=\"%s\" freq=%d psk=%s "
814 "go_dev_addr=" MACSTR
"%s", wpa_s
->ifname
,
815 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
816 ssid
->frequency
, psk
,
817 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
818 params
->persistent_group
? " [PERSISTENT]" :
822 if (params
->persistent_group
)
823 network_id
= wpas_p2p_store_persistent_group(
825 wpa_s
->global
->p2p_dev_addr
);
827 network_id
= ssid
->id
;
828 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
829 wpas_p2p_cross_connect_setup(wpa_s
);
830 wpas_p2p_set_group_idle_timeout(wpa_s
);
834 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
835 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
836 params
->peer_interface_addr
)) {
837 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
841 if (params
->wps_method
== WPS_PBC
)
842 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
843 params
->peer_device_addr
);
844 else if (wpa_s
->p2p_pin
[0])
845 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
846 wpa_s
->p2p_pin
, NULL
, 0, 0);
847 os_free(wpa_s
->go_params
);
848 wpa_s
->go_params
= NULL
;
852 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
853 struct p2p_go_neg_results
*params
,
856 struct wpa_ssid
*ssid
;
858 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Starting GO");
859 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0) {
860 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not copy GO Negotiation "
865 ssid
= wpa_config_add_network(wpa_s
->conf
);
867 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not add network for GO");
871 wpa_s
->show_group_started
= 0;
873 wpa_config_set_network_defaults(ssid
);
876 ssid
->p2p_persistent_group
= params
->persistent_group
;
877 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
879 ssid
->frequency
= params
->freq
;
880 ssid
->ht40
= params
->ht40
;
881 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
883 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
884 ssid
->ssid_len
= params
->ssid_len
;
886 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
887 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
888 ssid
->proto
= WPA_PROTO_RSN
;
889 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
890 if (os_strlen(params
->passphrase
) > 0) {
891 ssid
->passphrase
= os_strdup(params
->passphrase
);
892 if (ssid
->passphrase
== NULL
) {
893 wpa_msg(wpa_s
, MSG_ERROR
, "P2P: Failed to copy "
894 "passphrase for GO");
895 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
899 ssid
->passphrase
= NULL
;
900 ssid
->psk_set
= params
->psk_set
;
902 os_memcpy(ssid
->psk
, params
->psk
, sizeof(ssid
->psk
));
903 else if (ssid
->passphrase
)
904 wpa_config_update_psk(ssid
);
905 ssid
->ap_max_inactivity
= wpa_s
->parent
->conf
->p2p_go_max_inactivity
;
907 wpa_s
->ap_configured_cb
= p2p_go_configured
;
908 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
909 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
910 wpa_s
->connect_without_scan
= ssid
;
911 wpa_s
->reassociate
= 1;
912 wpa_s
->disconnected
= 0;
913 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Request scan (that will be skipped) to "
915 wpa_supplicant_req_scan(wpa_s
, 0, 0);
919 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
920 const struct wpa_supplicant
*src
)
922 struct wpa_config
*d
;
923 const struct wpa_config
*s
;
928 #define C(n) if (s->n) d->n = os_strdup(s->n)
937 os_memcpy(d
->device_type
, s
->device_type
, WPS_DEV_TYPE_LEN
);
938 os_memcpy(d
->sec_device_type
, s
->sec_device_type
,
939 sizeof(d
->sec_device_type
));
940 d
->num_sec_device_types
= s
->num_sec_device_types
;
942 d
->p2p_group_idle
= s
->p2p_group_idle
;
943 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
944 d
->persistent_reconnect
= s
->persistent_reconnect
;
945 d
->max_num_sta
= s
->max_num_sta
;
946 d
->pbc_in_m1
= s
->pbc_in_m1
;
950 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
951 enum wpa_driver_if_type type
)
953 char ifname
[120], force_ifname
[120];
955 if (wpa_s
->pending_interface_name
[0]) {
956 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
957 "- skip creation of a new one");
958 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
959 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
960 "unknown?! ifname='%s'",
961 wpa_s
->pending_interface_name
);
967 os_snprintf(ifname
, sizeof(ifname
), "p2p-%s-%d", wpa_s
->ifname
,
968 wpa_s
->p2p_group_idx
);
969 if (os_strlen(ifname
) >= IFNAMSIZ
&&
970 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
971 /* Try to avoid going over the IFNAMSIZ length limit */
972 os_snprintf(ifname
, sizeof(ifname
), "p2p-%d",
973 wpa_s
->p2p_group_idx
);
975 force_ifname
[0] = '\0';
977 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
979 wpa_s
->p2p_group_idx
++;
981 wpa_s
->pending_interface_type
= type
;
982 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
983 wpa_s
->pending_interface_addr
, NULL
) < 0) {
984 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
989 if (force_ifname
[0]) {
990 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
992 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
993 sizeof(wpa_s
->pending_interface_name
));
995 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
996 sizeof(wpa_s
->pending_interface_name
));
997 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
998 MACSTR
, wpa_s
->pending_interface_name
,
999 MAC2STR(wpa_s
->pending_interface_addr
));
1005 static void wpas_p2p_remove_pending_group_interface(
1006 struct wpa_supplicant
*wpa_s
)
1008 if (!wpa_s
->pending_interface_name
[0] ||
1009 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
1010 return; /* No pending virtual interface */
1012 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
1013 wpa_s
->pending_interface_name
);
1014 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
1015 wpa_s
->pending_interface_name
);
1016 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1017 wpa_s
->pending_interface_name
[0] = '\0';
1021 static struct wpa_supplicant
*
1022 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1024 struct wpa_interface iface
;
1025 struct wpa_supplicant
*group_wpa_s
;
1027 if (!wpa_s
->pending_interface_name
[0]) {
1028 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1029 if (!wpas_p2p_create_iface(wpa_s
))
1032 * Something has forced us to remove the pending interface; try
1033 * to create a new one and hope for the best that we will get
1034 * the same local address.
1036 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1037 WPA_IF_P2P_CLIENT
) < 0)
1041 os_memset(&iface
, 0, sizeof(iface
));
1042 iface
.ifname
= wpa_s
->pending_interface_name
;
1043 iface
.driver
= wpa_s
->driver
->name
;
1044 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1045 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1046 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1047 if (group_wpa_s
== NULL
) {
1048 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1049 "wpa_supplicant interface");
1052 wpa_s
->pending_interface_name
[0] = '\0';
1053 group_wpa_s
->parent
= wpa_s
;
1054 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1055 P2P_GROUP_INTERFACE_CLIENT
;
1056 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1058 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
1064 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
1067 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1068 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
1069 if (wpa_s
->global
->p2p
)
1070 p2p_group_formation_failed(wpa_s
->global
->p2p
);
1071 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1072 wpa_drv_p2p_group_formation_failed(wpa_s
);
1073 wpas_group_formation_completed(wpa_s
, 0);
1077 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
1079 struct wpa_supplicant
*wpa_s
= ctx
;
1081 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1082 wpa_drv_cancel_remain_on_channel(wpa_s
);
1083 wpa_s
->off_channel_freq
= 0;
1084 wpa_s
->roc_waiting_drv_freq
= 0;
1088 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_FAILURE
"status=%d",
1090 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
1091 wpas_p2p_remove_pending_group_interface(wpa_s
);
1095 if (wpa_s
->p2p_go_ht40
)
1098 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
);
1099 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
1101 if (res
->role_go
&& wpa_s
->p2p_persistent_id
>= 0) {
1102 struct wpa_ssid
*ssid
;
1103 ssid
= wpa_config_get_network(wpa_s
->conf
,
1104 wpa_s
->p2p_persistent_id
);
1105 if (ssid
&& ssid
->disabled
== 2 &&
1106 ssid
->mode
== WPAS_MODE_P2P_GO
&& ssid
->passphrase
) {
1107 size_t len
= os_strlen(ssid
->passphrase
);
1108 wpa_printf(MSG_DEBUG
, "P2P: Override passphrase based "
1109 "on requested persistent group");
1110 os_memcpy(res
->passphrase
, ssid
->passphrase
, len
);
1111 res
->passphrase
[len
] = '\0';
1115 if (wpa_s
->create_p2p_iface
) {
1116 struct wpa_supplicant
*group_wpa_s
=
1117 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
1118 if (group_wpa_s
== NULL
) {
1119 wpas_p2p_remove_pending_group_interface(wpa_s
);
1122 if (group_wpa_s
!= wpa_s
) {
1123 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
1124 sizeof(group_wpa_s
->p2p_pin
));
1125 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
1127 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1128 wpa_s
->pending_interface_name
[0] = '\0';
1129 group_wpa_s
->p2p_in_provisioning
= 1;
1132 wpas_start_wps_go(group_wpa_s
, res
, 1);
1134 wpas_start_wps_enrollee(group_wpa_s
, res
);
1136 wpa_s
->p2p_in_provisioning
= 1;
1137 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1140 wpas_start_wps_go(wpa_s
, res
, 1);
1142 wpas_start_wps_enrollee(ctx
, res
);
1145 wpa_s
->p2p_long_listen
= 0;
1146 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1148 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1149 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1150 (res
->peer_config_timeout
% 100) * 10000,
1151 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1155 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1157 struct wpa_supplicant
*wpa_s
= ctx
;
1158 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1159 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1161 wpas_notify_p2p_go_neg_req(wpa_s
, src
, dev_passwd_id
);
1165 void wpas_dev_found(void *ctx
, const u8
*addr
,
1166 const struct p2p_peer_info
*info
,
1169 #ifndef CONFIG_NO_STDOUT_DEBUG
1170 struct wpa_supplicant
*wpa_s
= ctx
;
1171 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1173 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1174 " p2p_dev_addr=" MACSTR
1175 " pri_dev_type=%s name='%s' config_methods=0x%x "
1176 "dev_capab=0x%x group_capab=0x%x",
1177 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
1178 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
1180 info
->device_name
, info
->config_methods
,
1181 info
->dev_capab
, info
->group_capab
);
1182 #endif /* CONFIG_NO_STDOUT_DEBUG */
1184 wpas_notify_p2p_device_found(ctx
, info
->p2p_device_addr
, new_device
);
1188 static void wpas_dev_lost(void *ctx
, const u8
*dev_addr
)
1190 struct wpa_supplicant
*wpa_s
= ctx
;
1192 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_LOST
1193 "p2p_dev_addr=" MACSTR
, MAC2STR(dev_addr
));
1195 wpas_notify_p2p_device_lost(wpa_s
, dev_addr
);
1199 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1200 unsigned int duration
,
1201 const struct wpabuf
*probe_resp_ie
)
1203 struct wpa_supplicant
*wpa_s
= ctx
;
1205 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1207 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1208 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1209 "report received Probe Request frames");
1213 wpa_s
->pending_listen_freq
= freq
;
1214 wpa_s
->pending_listen_duration
= duration
;
1216 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1217 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1218 "to remain on channel (%u MHz) for Listen "
1220 wpa_s
->pending_listen_freq
= 0;
1223 wpa_s
->off_channel_freq
= 0;
1224 wpa_s
->roc_waiting_drv_freq
= freq
;
1230 static void wpas_stop_listen(void *ctx
)
1232 struct wpa_supplicant
*wpa_s
= ctx
;
1233 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1234 wpa_drv_cancel_remain_on_channel(wpa_s
);
1235 wpa_s
->off_channel_freq
= 0;
1236 wpa_s
->roc_waiting_drv_freq
= 0;
1238 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
1239 wpa_drv_probe_req_report(wpa_s
, 0);
1243 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1245 struct wpa_supplicant
*wpa_s
= ctx
;
1246 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
), 1);
1251 * DNS Header section is used only to calculate compression pointers, so the
1252 * contents of this data does not matter, but the length needs to be reserved
1253 * in the virtual packet.
1255 #define DNS_HEADER_LEN 12
1258 * 27-octet in-memory packet from P2P specification containing two implied
1259 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1261 #define P2P_SD_IN_MEMORY_LEN 27
1263 static int p2p_sd_dns_uncompress_label(char **upos
, char *uend
, u8
*start
,
1264 u8
**spos
, const u8
*end
)
1266 while (*spos
< end
) {
1267 u8 val
= ((*spos
)[0] & 0xc0) >> 6;
1270 if (val
== 1 || val
== 2) {
1271 /* These are reserved values in RFC 1035 */
1272 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
1273 "sequence starting with 0x%x", val
);
1282 if (*spos
+ 2 > end
) {
1283 wpa_printf(MSG_DEBUG
, "P2P: No room for full "
1284 "DNS offset field");
1288 offset
= (((*spos
)[0] & 0x3f) << 8) | (*spos
)[1];
1289 if (offset
>= *spos
- start
) {
1290 wpa_printf(MSG_DEBUG
, "P2P: Invalid DNS "
1291 "pointer offset %u", offset
);
1296 spos_tmp
= start
+ offset
;
1297 return p2p_sd_dns_uncompress_label(upos
, uend
, start
,
1303 len
= (*spos
)[0] & 0x3f;
1308 if (*spos
+ len
> end
) {
1309 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
1310 "sequence - no room for label with length "
1315 if (*upos
+ len
+ 2 > uend
)
1318 os_memcpy(*upos
, *spos
, len
);
1330 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1331 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1332 * not large enough */
1333 static int p2p_sd_dns_uncompress(char *buf
, size_t buf_len
, const u8
*msg
,
1334 size_t msg_len
, size_t offset
)
1336 /* 27-octet in-memory packet from P2P specification */
1337 const char *prefix
= "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1338 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1339 u8
*tmp
, *end
, *spos
;
1345 if (offset
> msg_len
)
1348 tmp
= os_malloc(DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
+ msg_len
);
1351 spos
= tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
;
1352 end
= spos
+ msg_len
;
1355 os_memset(tmp
, 0, DNS_HEADER_LEN
);
1356 os_memcpy(tmp
+ DNS_HEADER_LEN
, prefix
, P2P_SD_IN_MEMORY_LEN
);
1357 os_memcpy(tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
, msg
, msg_len
);
1360 uend
= buf
+ buf_len
;
1362 ret
= p2p_sd_dns_uncompress_label(&upos
, uend
, tmp
, &spos
, end
);
1371 } else if (upos
[-1] == '.')
1379 static struct p2p_srv_bonjour
*
1380 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1381 const struct wpabuf
*query
)
1383 struct p2p_srv_bonjour
*bsrv
;
1386 len
= wpabuf_len(query
);
1387 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1388 struct p2p_srv_bonjour
, list
) {
1389 if (len
== wpabuf_len(bsrv
->query
) &&
1390 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1398 static struct p2p_srv_upnp
*
1399 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1400 const char *service
)
1402 struct p2p_srv_upnp
*usrv
;
1404 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1405 struct p2p_srv_upnp
, list
) {
1406 if (version
== usrv
->version
&&
1407 os_strcmp(service
, usrv
->service
) == 0)
1414 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1419 if (wpabuf_tailroom(resp
) < 5)
1422 /* Length (to be filled) */
1423 len_pos
= wpabuf_put(resp
, 2);
1424 wpabuf_put_u8(resp
, srv_proto
);
1425 wpabuf_put_u8(resp
, srv_trans_id
);
1427 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1428 /* Response Data: empty */
1429 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1433 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1434 struct wpabuf
*resp
, u8 srv_trans_id
)
1436 struct p2p_srv_bonjour
*bsrv
;
1439 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1441 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1442 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1446 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1447 struct p2p_srv_bonjour
, list
) {
1448 if (wpabuf_tailroom(resp
) <
1449 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1451 /* Length (to be filled) */
1452 len_pos
= wpabuf_put(resp
, 2);
1453 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1454 wpabuf_put_u8(resp
, srv_trans_id
);
1456 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1457 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1458 wpabuf_head(bsrv
->resp
),
1459 wpabuf_len(bsrv
->resp
));
1461 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1462 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1463 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1469 static int match_bonjour_query(struct p2p_srv_bonjour
*bsrv
, const u8
*query
,
1472 char str_rx
[256], str_srv
[256];
1474 if (query_len
< 3 || wpabuf_len(bsrv
->query
) < 3)
1475 return 0; /* Too short to include DNS Type and Version */
1476 if (os_memcmp(query
+ query_len
- 3,
1477 wpabuf_head_u8(bsrv
->query
) + wpabuf_len(bsrv
->query
) - 3,
1479 return 0; /* Mismatch in DNS Type or Version */
1480 if (query_len
== wpabuf_len(bsrv
->query
) &&
1481 os_memcmp(query
, wpabuf_head(bsrv
->query
), query_len
- 3) == 0)
1482 return 1; /* Binary match */
1484 if (p2p_sd_dns_uncompress(str_rx
, sizeof(str_rx
), query
, query_len
- 3,
1486 return 0; /* Failed to uncompress query */
1487 if (p2p_sd_dns_uncompress(str_srv
, sizeof(str_srv
),
1488 wpabuf_head(bsrv
->query
),
1489 wpabuf_len(bsrv
->query
) - 3, 0))
1490 return 0; /* Failed to uncompress service */
1492 return os_strcmp(str_rx
, str_srv
) == 0;
1496 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1497 struct wpabuf
*resp
, u8 srv_trans_id
,
1498 const u8
*query
, size_t query_len
)
1500 struct p2p_srv_bonjour
*bsrv
;
1504 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1506 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1507 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1508 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1513 if (query_len
== 0) {
1514 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1518 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1519 struct p2p_srv_bonjour
, list
) {
1520 if (!match_bonjour_query(bsrv
, query
, query_len
))
1523 if (wpabuf_tailroom(resp
) <
1524 5 + query_len
+ wpabuf_len(bsrv
->resp
))
1529 /* Length (to be filled) */
1530 len_pos
= wpabuf_put(resp
, 2);
1531 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1532 wpabuf_put_u8(resp
, srv_trans_id
);
1535 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1536 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1537 wpabuf_head(bsrv
->resp
),
1538 wpabuf_len(bsrv
->resp
));
1541 wpabuf_put_data(resp
, query
, query_len
); /* Key */
1542 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1544 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1548 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1550 if (wpabuf_tailroom(resp
) < 5)
1553 /* Length (to be filled) */
1554 len_pos
= wpabuf_put(resp
, 2);
1555 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1556 wpabuf_put_u8(resp
, srv_trans_id
);
1559 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1560 /* Response Data: empty */
1561 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1567 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1568 struct wpabuf
*resp
, u8 srv_trans_id
)
1570 struct p2p_srv_upnp
*usrv
;
1573 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1575 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1576 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1580 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1581 struct p2p_srv_upnp
, list
) {
1582 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1585 /* Length (to be filled) */
1586 len_pos
= wpabuf_put(resp
, 2);
1587 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1588 wpabuf_put_u8(resp
, srv_trans_id
);
1591 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1593 wpabuf_put_u8(resp
, usrv
->version
);
1594 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1596 wpabuf_put_str(resp
, usrv
->service
);
1597 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1603 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1604 struct wpabuf
*resp
, u8 srv_trans_id
,
1605 const u8
*query
, size_t query_len
)
1607 struct p2p_srv_upnp
*usrv
;
1613 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1616 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1617 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1618 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1623 if (query_len
== 0) {
1624 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1628 if (wpabuf_tailroom(resp
) < 5)
1631 /* Length (to be filled) */
1632 len_pos
= wpabuf_put(resp
, 2);
1633 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1634 wpabuf_put_u8(resp
, srv_trans_id
);
1637 str
= os_malloc(query_len
);
1640 os_memcpy(str
, query
+ 1, query_len
- 1);
1641 str
[query_len
- 1] = '\0';
1643 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1644 struct p2p_srv_upnp
, list
) {
1645 if (version
!= usrv
->version
)
1648 if (os_strcmp(str
, "ssdp:all") != 0 &&
1649 os_strstr(usrv
->service
, str
) == NULL
)
1652 if (wpabuf_tailroom(resp
) < 2)
1656 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1658 wpabuf_put_u8(resp
, version
);
1660 wpabuf_put_u8(resp
, ',');
1664 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1666 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1668 wpabuf_put_str(resp
, usrv
->service
);
1673 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1676 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1677 /* Response Data: empty */
1680 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1684 #ifdef CONFIG_WIFI_DISPLAY
1685 static void wpas_sd_req_wfd(struct wpa_supplicant
*wpa_s
,
1686 struct wpabuf
*resp
, u8 srv_trans_id
,
1687 const u8
*query
, size_t query_len
)
1693 wpa_hexdump(MSG_DEBUG
, "P2P: SD Request for WFD", query
, query_len
);
1695 if (!wpa_s
->global
->wifi_display
) {
1696 wpa_printf(MSG_DEBUG
, "P2P: WFD protocol not available");
1697 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_WIFI_DISPLAY
,
1702 if (query_len
< 1) {
1703 wpa_printf(MSG_DEBUG
, "P2P: Missing WFD Requested Device "
1708 if (wpabuf_tailroom(resp
) < 5)
1713 wpa_printf(MSG_DEBUG
, "P2P: WSD for device role 0x%x", role
);
1715 /* TODO: role specific handling */
1717 /* Length (to be filled) */
1718 len_pos
= wpabuf_put(resp
, 2);
1719 wpabuf_put_u8(resp
, P2P_SERV_WIFI_DISPLAY
);
1720 wpabuf_put_u8(resp
, srv_trans_id
);
1721 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
); /* Status Code */
1723 while (pos
< query
+ query_len
) {
1724 if (*pos
< MAX_WFD_SUBELEMS
&&
1725 wpa_s
->global
->wfd_subelem
[*pos
] &&
1726 wpabuf_tailroom(resp
) >=
1727 wpabuf_len(wpa_s
->global
->wfd_subelem
[*pos
])) {
1728 wpa_printf(MSG_DEBUG
, "P2P: Add WSD response "
1729 "subelement %u", *pos
);
1730 wpabuf_put_buf(resp
, wpa_s
->global
->wfd_subelem
[*pos
]);
1735 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1737 #endif /* CONFIG_WIFI_DISPLAY */
1740 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
1741 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
1743 struct wpa_supplicant
*wpa_s
= ctx
;
1744 const u8
*pos
= tlvs
;
1745 const u8
*end
= tlvs
+ tlvs_len
;
1748 struct wpabuf
*resp
;
1749 u8 srv_proto
, srv_trans_id
;
1753 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
1755 buf_len
= 2 * tlvs_len
+ 1;
1756 buf
= os_malloc(buf_len
);
1758 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1759 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
1761 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
1766 if (wpa_s
->p2p_sd_over_ctrl_iface
) {
1767 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
1768 update_indic
, tlvs
, tlvs_len
);
1769 return; /* to be processed by an external program */
1772 resp
= wpabuf_alloc(10000);
1776 while (pos
+ 1 < end
) {
1777 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
1778 slen
= WPA_GET_LE16(pos
);
1780 if (pos
+ slen
> end
|| slen
< 2) {
1781 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
1786 tlv_end
= pos
+ slen
;
1789 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1791 srv_trans_id
= *pos
++;
1792 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1795 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
1796 pos
, tlv_end
- pos
);
1799 if (wpa_s
->force_long_sd
) {
1800 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
1802 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1803 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1807 switch (srv_proto
) {
1808 case P2P_SERV_ALL_SERVICES
:
1809 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
1810 "for all services");
1811 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
1812 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1813 wpa_printf(MSG_DEBUG
, "P2P: No service "
1814 "discovery protocols available");
1815 wpas_sd_add_proto_not_avail(
1816 resp
, P2P_SERV_ALL_SERVICES
,
1820 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1821 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1823 case P2P_SERV_BONJOUR
:
1824 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
1825 pos
, tlv_end
- pos
);
1828 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
1829 pos
, tlv_end
- pos
);
1831 #ifdef CONFIG_WIFI_DISPLAY
1832 case P2P_SERV_WIFI_DISPLAY
:
1833 wpas_sd_req_wfd(wpa_s
, resp
, srv_trans_id
,
1834 pos
, tlv_end
- pos
);
1836 #endif /* CONFIG_WIFI_DISPLAY */
1838 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
1839 "protocol %u", srv_proto
);
1840 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
1849 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
1850 update_indic
, tlvs
, tlvs_len
);
1852 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
1858 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
1859 const u8
*tlvs
, size_t tlvs_len
)
1861 struct wpa_supplicant
*wpa_s
= ctx
;
1862 const u8
*pos
= tlvs
;
1863 const u8
*end
= tlvs
+ tlvs_len
;
1869 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
1871 if (tlvs_len
> 1500) {
1872 /* TODO: better way for handling this */
1873 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1874 P2P_EVENT_SERV_DISC_RESP MACSTR
1875 " %u <long response: %u bytes>",
1876 MAC2STR(sa
), update_indic
,
1877 (unsigned int) tlvs_len
);
1879 buf_len
= 2 * tlvs_len
+ 1;
1880 buf
= os_malloc(buf_len
);
1882 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1883 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1884 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
1885 MAC2STR(sa
), update_indic
, buf
);
1891 u8 srv_proto
, srv_trans_id
, status
;
1893 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
1894 slen
= WPA_GET_LE16(pos
);
1896 if (pos
+ slen
> end
|| slen
< 3) {
1897 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
1901 tlv_end
= pos
+ slen
;
1904 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1906 srv_trans_id
= *pos
++;
1907 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1910 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
1913 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
1914 pos
, tlv_end
- pos
);
1919 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
1923 u64
wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1924 const struct wpabuf
*tlvs
)
1926 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1927 return wpa_drv_p2p_sd_request(wpa_s
, dst
, tlvs
);
1928 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1930 return (uintptr_t) p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
1934 u64
wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1935 u8 version
, const char *query
)
1937 struct wpabuf
*tlvs
;
1940 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
1943 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
1944 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
1945 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
1946 wpabuf_put_u8(tlvs
, version
);
1947 wpabuf_put_str(tlvs
, query
);
1948 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
1954 #ifdef CONFIG_WIFI_DISPLAY
1956 static u64
wpas_p2p_sd_request_wfd(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1957 const struct wpabuf
*tlvs
)
1959 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1961 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1963 return (uintptr_t) p2p_sd_request_wfd(wpa_s
->global
->p2p
, dst
, tlvs
);
1967 #define MAX_WFD_SD_SUBELEMS 20
1969 static void wfd_add_sd_req_role(struct wpabuf
*tlvs
, u8 id
, u8 role
,
1970 const char *subelems
)
1977 len
= wpabuf_put(tlvs
, 2);
1978 wpabuf_put_u8(tlvs
, P2P_SERV_WIFI_DISPLAY
); /* Service Protocol Type */
1979 wpabuf_put_u8(tlvs
, id
); /* Service Transaction ID */
1981 wpabuf_put_u8(tlvs
, role
);
1986 if (val
>= 0 && val
< 256) {
1987 wpabuf_put_u8(tlvs
, val
);
1989 if (count
== MAX_WFD_SD_SUBELEMS
)
1992 pos
= os_strchr(pos
+ 1, ',');
1998 WPA_PUT_LE16(len
, (u8
*) wpabuf_put(tlvs
, 0) - len
- 2);
2002 u64
wpas_p2p_sd_request_wifi_display(struct wpa_supplicant
*wpa_s
,
2003 const u8
*dst
, const char *role
)
2005 struct wpabuf
*tlvs
;
2007 const char *subelems
;
2010 subelems
= os_strchr(role
, ' ');
2011 if (subelems
== NULL
)
2015 tlvs
= wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS
));
2019 if (os_strstr(role
, "[source]"))
2020 wfd_add_sd_req_role(tlvs
, id
++, 0x00, subelems
);
2021 if (os_strstr(role
, "[pri-sink]"))
2022 wfd_add_sd_req_role(tlvs
, id
++, 0x01, subelems
);
2023 if (os_strstr(role
, "[sec-sink]"))
2024 wfd_add_sd_req_role(tlvs
, id
++, 0x02, subelems
);
2025 if (os_strstr(role
, "[source+sink]"))
2026 wfd_add_sd_req_role(tlvs
, id
++, 0x03, subelems
);
2028 ret
= wpas_p2p_sd_request_wfd(wpa_s
, dst
, tlvs
);
2033 #endif /* CONFIG_WIFI_DISPLAY */
2036 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, u64 req
)
2038 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2039 return wpa_drv_p2p_sd_cancel_request(wpa_s
, req
);
2040 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2042 return p2p_sd_cancel_request(wpa_s
->global
->p2p
,
2043 (void *) (uintptr_t) req
);
2047 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
2048 const u8
*dst
, u8 dialog_token
,
2049 const struct wpabuf
*resp_tlvs
)
2051 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2052 wpa_drv_p2p_sd_response(wpa_s
, freq
, dst
, dialog_token
,
2056 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2058 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
2063 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
2065 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2066 wpa_drv_p2p_service_update(wpa_s
);
2069 if (wpa_s
->global
->p2p
)
2070 p2p_sd_service_update(wpa_s
->global
->p2p
);
2074 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
2076 dl_list_del(&bsrv
->list
);
2077 wpabuf_free(bsrv
->query
);
2078 wpabuf_free(bsrv
->resp
);
2083 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
2085 dl_list_del(&usrv
->list
);
2086 os_free(usrv
->service
);
2091 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
2093 struct p2p_srv_bonjour
*bsrv
, *bn
;
2094 struct p2p_srv_upnp
*usrv
, *un
;
2096 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
2097 struct p2p_srv_bonjour
, list
)
2098 wpas_p2p_srv_bonjour_free(bsrv
);
2100 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
2101 struct p2p_srv_upnp
, list
)
2102 wpas_p2p_srv_upnp_free(usrv
);
2104 wpas_p2p_sd_service_update(wpa_s
);
2108 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
2109 struct wpabuf
*query
, struct wpabuf
*resp
)
2111 struct p2p_srv_bonjour
*bsrv
;
2113 bsrv
= os_zalloc(sizeof(*bsrv
));
2116 bsrv
->query
= query
;
2118 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
2120 wpas_p2p_sd_service_update(wpa_s
);
2125 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
2126 const struct wpabuf
*query
)
2128 struct p2p_srv_bonjour
*bsrv
;
2130 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
2133 wpas_p2p_srv_bonjour_free(bsrv
);
2134 wpas_p2p_sd_service_update(wpa_s
);
2139 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2140 const char *service
)
2142 struct p2p_srv_upnp
*usrv
;
2144 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
2145 return 0; /* Already listed */
2146 usrv
= os_zalloc(sizeof(*usrv
));
2149 usrv
->version
= version
;
2150 usrv
->service
= os_strdup(service
);
2151 if (usrv
->service
== NULL
) {
2155 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
2157 wpas_p2p_sd_service_update(wpa_s
);
2162 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2163 const char *service
)
2165 struct p2p_srv_upnp
*usrv
;
2167 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
2170 wpas_p2p_srv_upnp_free(usrv
);
2171 wpas_p2p_sd_service_update(wpa_s
);
2176 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
2177 const u8
*peer
, const char *params
,
2178 unsigned int generated_pin
)
2180 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
" %08d%s",
2181 MAC2STR(peer
), generated_pin
, params
);
2185 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
2186 const u8
*peer
, const char *params
)
2188 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
"%s",
2189 MAC2STR(peer
), params
);
2193 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
2194 const u8
*dev_addr
, const u8
*pri_dev_type
,
2195 const char *dev_name
, u16 supp_config_methods
,
2196 u8 dev_capab
, u8 group_capab
, const u8
*group_id
,
2197 size_t group_id_len
)
2199 struct wpa_supplicant
*wpa_s
= ctx
;
2200 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
2202 u8 empty_dev_type
[8];
2203 unsigned int generated_pin
= 0;
2204 struct wpa_supplicant
*group
= NULL
;
2207 for (group
= wpa_s
->global
->ifaces
; group
; group
= group
->next
)
2209 struct wpa_ssid
*s
= group
->current_ssid
;
2211 s
->mode
== WPAS_MODE_P2P_GO
&&
2212 group_id_len
- ETH_ALEN
== s
->ssid_len
&&
2213 os_memcmp(group_id
+ ETH_ALEN
, s
->ssid
,
2219 if (pri_dev_type
== NULL
) {
2220 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
2221 pri_dev_type
= empty_dev_type
;
2223 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
2224 " pri_dev_type=%s name='%s' config_methods=0x%x "
2225 "dev_capab=0x%x group_capab=0x%x%s%s",
2227 wps_dev_type_bin2str(pri_dev_type
, devtype
,
2229 dev_name
, supp_config_methods
, dev_capab
, group_capab
,
2230 group
? " group=" : "",
2231 group
? group
->ifname
: "");
2232 params
[sizeof(params
) - 1] = '\0';
2234 if (config_methods
& WPS_CONFIG_DISPLAY
) {
2235 generated_pin
= wps_generate_pin();
2236 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
2238 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
2239 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
2240 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
2241 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2242 "%s", MAC2STR(peer
), params
);
2244 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 1 /* request */,
2245 P2P_PROV_DISC_SUCCESS
,
2246 config_methods
, generated_pin
);
2250 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
2252 struct wpa_supplicant
*wpa_s
= ctx
;
2253 unsigned int generated_pin
= 0;
2256 if (wpa_s
->pending_pd_before_join
&&
2257 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
2258 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
2259 wpa_s
->pending_pd_before_join
= 0;
2260 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
2261 "join-existing-group operation");
2262 wpas_p2p_join_start(wpa_s
);
2266 if (wpa_s
->pending_pd_use
== AUTO_PD_JOIN
||
2267 wpa_s
->pending_pd_use
== AUTO_PD_GO_NEG
)
2268 os_snprintf(params
, sizeof(params
), " peer_go=%d",
2269 wpa_s
->pending_pd_use
== AUTO_PD_JOIN
);
2273 if (config_methods
& WPS_CONFIG_DISPLAY
)
2274 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
2275 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
2276 generated_pin
= wps_generate_pin();
2277 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
2279 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
2280 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2281 "%s", MAC2STR(peer
), params
);
2283 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
2284 P2P_PROV_DISC_SUCCESS
,
2285 config_methods
, generated_pin
);
2289 static void wpas_prov_disc_fail(void *ctx
, const u8
*peer
,
2290 enum p2p_prov_disc_status status
)
2292 struct wpa_supplicant
*wpa_s
= ctx
;
2294 if (wpa_s
->p2p_fallback_to_go_neg
) {
2295 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: PD for p2p_connect-auto "
2296 "failed - fall back to GO Negotiation");
2297 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
2301 if (status
== P2P_PROV_DISC_TIMEOUT_JOIN
) {
2302 wpa_s
->pending_pd_before_join
= 0;
2303 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
2304 "join-existing-group operation (no ACK for PD "
2306 wpas_p2p_join_start(wpa_s
);
2310 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
2311 " p2p_dev_addr=" MACSTR
" status=%d",
2312 MAC2STR(peer
), status
);
2314 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
2319 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
2320 const u8
*go_dev_addr
, const u8
*ssid
,
2321 size_t ssid_len
, int *go
, u8
*group_bssid
,
2322 int *force_freq
, int persistent_group
)
2324 struct wpa_supplicant
*wpa_s
= ctx
;
2326 u8 cur_bssid
[ETH_ALEN
];
2328 struct wpa_supplicant
*grp
;
2330 if (!persistent_group
) {
2331 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
2332 " to join an active group", MAC2STR(sa
));
2333 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
2334 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
2336 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
2337 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
2338 "authorized invitation");
2342 * Do not accept the invitation automatically; notify user and
2345 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
2348 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
2350 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
2351 "running persistent group");
2353 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
2357 if (!wpa_s
->conf
->persistent_reconnect
)
2358 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
2360 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2361 if (s
->disabled
== 2 &&
2362 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
2363 s
->ssid_len
== ssid_len
&&
2364 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2369 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
2370 " requested reinvocation of an unknown group",
2372 return P2P_SC_FAIL_UNKNOWN_GROUP
;
2375 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
2377 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
2378 wpa_printf(MSG_DEBUG
, "P2P: The only available "
2379 "interface is already in use - reject "
2381 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
2383 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
2384 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
2386 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
2388 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2389 "interface address for the group");
2390 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
2392 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
2397 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, cur_bssid
) == 0 &&
2398 wpa_s
->assoc_freq
) {
2399 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
2400 "the channel we are already using");
2401 *force_freq
= wpa_s
->assoc_freq
;
2404 res
= wpa_drv_shared_freq(wpa_s
);
2406 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
2407 "with the channel we are already using on a "
2408 "shared interface");
2412 return P2P_SC_SUCCESS
;
2416 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
2417 const u8
*ssid
, size_t ssid_len
,
2418 const u8
*go_dev_addr
, u8 status
,
2421 struct wpa_supplicant
*wpa_s
= ctx
;
2424 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2425 if (s
->disabled
== 2 &&
2426 s
->ssid_len
== ssid_len
&&
2427 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2431 if (status
== P2P_SC_SUCCESS
) {
2432 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2433 " was accepted; op_freq=%d MHz",
2434 MAC2STR(sa
), op_freq
);
2436 int go
= s
->mode
== WPAS_MODE_P2P_GO
;
2437 wpas_p2p_group_add_persistent(
2438 wpa_s
, s
, go
, go
? op_freq
: 0, 0);
2440 wpa_s
->user_initiated_pd
= 0;
2441 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
2442 wpa_s
->p2p_wps_method
, 0);
2447 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2448 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2449 " was rejected (status %u)", MAC2STR(sa
), status
);
2455 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2456 "sa=" MACSTR
" go_dev_addr=" MACSTR
2457 " bssid=" MACSTR
" unknown-network",
2458 MAC2STR(sa
), MAC2STR(go_dev_addr
),
2461 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2462 "sa=" MACSTR
" go_dev_addr=" MACSTR
2464 MAC2STR(sa
), MAC2STR(go_dev_addr
));
2469 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
"sa=" MACSTR
2470 " persistent=%d", MAC2STR(sa
), s
->id
);
2474 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
)
2476 struct wpa_supplicant
*wpa_s
= ctx
;
2477 struct wpa_ssid
*ssid
;
2480 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2481 "status=%d " MACSTR
,
2482 status
, MAC2STR(bssid
));
2484 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2485 "status=%d ", status
);
2487 wpas_notify_p2p_invitation_result(wpa_s
, status
, bssid
);
2489 if (wpa_s
->pending_invite_ssid_id
== -1)
2490 return; /* Invitation to active group */
2492 if (status
!= P2P_SC_SUCCESS
) {
2493 wpas_p2p_remove_pending_group_interface(wpa_s
);
2497 ssid
= wpa_config_get_network(wpa_s
->conf
,
2498 wpa_s
->pending_invite_ssid_id
);
2500 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
2501 "data matching with invitation");
2506 * The peer could have missed our ctrl::ack frame for Invitation
2507 * Response and continue retransmitting the frame. To reduce the
2508 * likelihood of the peer not getting successful TX status for the
2509 * Invitation Response frame, wait a short time here before starting
2510 * the persistent group so that we will remain on the current channel to
2511 * acknowledge any possible retransmission from the peer.
2513 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: 50 ms wait on current channel before "
2514 "starting persistent group");
2517 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
2518 ssid
->mode
== WPAS_MODE_P2P_GO
,
2519 wpa_s
->p2p_persistent_go_freq
,
2520 wpa_s
->p2p_go_ht40
);
2524 static int wpas_p2p_disallowed_freq(struct wpa_global
*global
,
2529 if (global
->p2p_disallow_freq
== NULL
)
2532 for (i
= 0; i
< global
->num_p2p_disallow_freq
; i
++) {
2533 if (freq
>= global
->p2p_disallow_freq
[i
].min
&&
2534 freq
<= global
->p2p_disallow_freq
[i
].max
)
2542 static void wpas_p2p_add_chan(struct p2p_reg_class
*reg
, u8 chan
)
2544 reg
->channel
[reg
->channels
] = chan
;
2549 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
2550 struct p2p_channels
*chan
)
2554 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
2557 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2558 chan
->reg_class
[cla
].reg_class
= 81;
2559 chan
->reg_class
[cla
].channels
= 0;
2560 for (i
= 0; i
< 11; i
++) {
2561 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 2412 + i
* 5))
2562 wpas_p2p_add_chan(&chan
->reg_class
[cla
], i
+ 1);
2564 if (chan
->reg_class
[cla
].channels
)
2567 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
2570 /* Operating class 115 - 5 GHz, channels 36-48 */
2571 chan
->reg_class
[cla
].reg_class
= 115;
2572 chan
->reg_class
[cla
].channels
= 0;
2573 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 36 * 5))
2574 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 36);
2575 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 40 * 5))
2576 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 40);
2577 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 44 * 5))
2578 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 44);
2579 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 48 * 5))
2580 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 48);
2581 if (chan
->reg_class
[cla
].channels
)
2584 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
2587 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2588 chan
->reg_class
[cla
].reg_class
= 124;
2589 chan
->reg_class
[cla
].channels
= 0;
2590 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 149 * 5))
2591 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 149);
2592 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 153 * 5))
2593 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 153);
2594 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 156 * 5))
2595 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 157);
2596 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 161 * 5))
2597 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 161);
2598 if (chan
->reg_class
[cla
].channels
)
2601 chan
->reg_classes
= cla
;
2606 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
2608 enum hostapd_hw_mode mode
)
2612 for (i
= 0; i
< num_modes
; i
++) {
2613 if (modes
[i
].mode
== mode
)
2621 static int has_channel(struct wpa_global
*global
,
2622 struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
2627 freq
= (mode
->mode
== HOSTAPD_MODE_IEEE80211A
? 5000 : 2407) +
2629 if (wpas_p2p_disallowed_freq(global
, freq
))
2632 for (i
= 0; i
< mode
->num_channels
; i
++) {
2633 if (mode
->channels
[i
].chan
== chan
) {
2635 *flags
= mode
->channels
[i
].flag
;
2636 return !(mode
->channels
[i
].flag
&
2637 (HOSTAPD_CHAN_DISABLED
|
2638 HOSTAPD_CHAN_PASSIVE_SCAN
|
2639 HOSTAPD_CHAN_NO_IBSS
|
2640 HOSTAPD_CHAN_RADAR
));
2648 struct p2p_oper_class_map
{
2649 enum hostapd_hw_mode mode
;
2654 enum { BW20
, BW40PLUS
, BW40MINUS
} bw
;
2657 static struct p2p_oper_class_map op_class
[] = {
2658 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
2659 #if 0 /* Do not enable HT40 on 2 GHz for now */
2660 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
2661 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
2663 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
2664 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
2665 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
2666 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
2667 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
2668 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
2669 { -1, 0, 0, 0, 0, BW20
}
2673 static int wpas_p2p_verify_channel(struct wpa_supplicant
*wpa_s
,
2674 struct hostapd_hw_modes
*mode
,
2679 if (!has_channel(wpa_s
->global
, mode
, channel
, &flag
))
2681 if (bw
== BW40MINUS
&&
2682 (!(flag
& HOSTAPD_CHAN_HT40MINUS
) ||
2683 !has_channel(wpa_s
->global
, mode
, channel
- 4, NULL
)))
2685 if (bw
== BW40PLUS
&&
2686 (!(flag
& HOSTAPD_CHAN_HT40PLUS
) ||
2687 !has_channel(wpa_s
->global
, mode
, channel
+ 4, NULL
)))
2693 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
2694 struct p2p_channels
*chan
)
2696 struct hostapd_hw_modes
*mode
;
2699 if (wpa_s
->hw
.modes
== NULL
) {
2700 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
2701 "of all supported channels; assume dualband "
2703 return wpas_p2p_default_channels(wpa_s
, chan
);
2708 for (op
= 0; op_class
[op
].op_class
; op
++) {
2709 struct p2p_oper_class_map
*o
= &op_class
[op
];
2711 struct p2p_reg_class
*reg
= NULL
;
2713 mode
= get_mode(wpa_s
->hw
.modes
, wpa_s
->hw
.num_modes
, o
->mode
);
2716 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2717 if (wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
) < 1)
2720 wpa_printf(MSG_DEBUG
, "P2P: Add operating "
2721 "class %u", o
->op_class
);
2722 reg
= &chan
->reg_class
[cla
];
2724 reg
->reg_class
= o
->op_class
;
2726 reg
->channel
[reg
->channels
] = ch
;
2730 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
2731 reg
->channel
, reg
->channels
);
2735 chan
->reg_classes
= cla
;
2741 int wpas_p2p_get_ht40_mode(struct wpa_supplicant
*wpa_s
,
2742 struct hostapd_hw_modes
*mode
, u8 channel
)
2746 for (op
= 0; op_class
[op
].op_class
; op
++) {
2747 struct p2p_oper_class_map
*o
= &op_class
[op
];
2750 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2751 if (o
->mode
!= HOSTAPD_MODE_IEEE80211A
||
2752 o
->bw
== BW20
|| ch
!= channel
)
2754 ret
= wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
);
2758 return (o
->bw
== BW40MINUS
) ? -1 : 1;
2767 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
2770 struct wpa_supplicant
*wpa_s
= ctx
;
2772 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2773 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
2779 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
2783 static int wpas_go_connected(void *ctx
, const u8
*dev_addr
)
2785 struct wpa_supplicant
*wpa_s
= ctx
;
2787 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2788 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
2791 if (ssid
->mode
!= WPAS_MODE_INFRA
)
2793 if (wpa_s
->wpa_state
!= WPA_COMPLETED
&&
2794 wpa_s
->wpa_state
!= WPA_GROUP_HANDSHAKE
)
2796 if (os_memcmp(wpa_s
->go_dev_addr
, dev_addr
, ETH_ALEN
) == 0)
2805 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2806 * @global: Pointer to global data from wpa_supplicant_init()
2807 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2808 * Returns: 0 on success, -1 on failure
2810 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
2812 struct p2p_config p2p
;
2816 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
2822 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2823 struct p2p_params params
;
2825 wpa_printf(MSG_DEBUG
, "P2P: Use driver-based P2P management");
2826 os_memset(¶ms
, 0, sizeof(params
));
2827 params
.dev_name
= wpa_s
->conf
->device_name
;
2828 os_memcpy(params
.pri_dev_type
, wpa_s
->conf
->device_type
,
2830 params
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2831 os_memcpy(params
.sec_dev_type
,
2832 wpa_s
->conf
->sec_device_type
,
2833 params
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2835 if (wpa_drv_p2p_set_params(wpa_s
, ¶ms
) < 0)
2841 os_memset(&p2p
, 0, sizeof(p2p
));
2842 p2p
.msg_ctx
= wpa_s
;
2844 p2p
.p2p_scan
= wpas_p2p_scan
;
2845 p2p
.send_action
= wpas_send_action
;
2846 p2p
.send_action_done
= wpas_send_action_done
;
2847 p2p
.go_neg_completed
= wpas_go_neg_completed
;
2848 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
2849 p2p
.dev_found
= wpas_dev_found
;
2850 p2p
.dev_lost
= wpas_dev_lost
;
2851 p2p
.start_listen
= wpas_start_listen
;
2852 p2p
.stop_listen
= wpas_stop_listen
;
2853 p2p
.send_probe_resp
= wpas_send_probe_resp
;
2854 p2p
.sd_request
= wpas_sd_request
;
2855 p2p
.sd_response
= wpas_sd_response
;
2856 p2p
.prov_disc_req
= wpas_prov_disc_req
;
2857 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
2858 p2p
.prov_disc_fail
= wpas_prov_disc_fail
;
2859 p2p
.invitation_process
= wpas_invitation_process
;
2860 p2p
.invitation_received
= wpas_invitation_received
;
2861 p2p
.invitation_result
= wpas_invitation_result
;
2862 p2p
.get_noa
= wpas_get_noa
;
2863 p2p
.go_connected
= wpas_go_connected
;
2865 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2866 os_memcpy(p2p
.dev_addr
, wpa_s
->global
->p2p_dev_addr
, ETH_ALEN
);
2867 p2p
.dev_name
= wpa_s
->conf
->device_name
;
2868 p2p
.manufacturer
= wpa_s
->conf
->manufacturer
;
2869 p2p
.model_name
= wpa_s
->conf
->model_name
;
2870 p2p
.model_number
= wpa_s
->conf
->model_number
;
2871 p2p
.serial_number
= wpa_s
->conf
->serial_number
;
2873 os_memcpy(p2p
.uuid
, wpa_s
->wps
->uuid
, 16);
2874 p2p
.config_methods
= wpa_s
->wps
->config_methods
;
2877 if (wpa_s
->conf
->p2p_listen_reg_class
&&
2878 wpa_s
->conf
->p2p_listen_channel
) {
2879 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
2880 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
2884 * Pick one of the social channels randomly as the listen
2887 os_get_random((u8
*) &r
, sizeof(r
));
2888 p2p
.channel
= 1 + (r
% 3) * 5;
2890 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
2892 if (wpa_s
->conf
->p2p_oper_reg_class
&&
2893 wpa_s
->conf
->p2p_oper_channel
) {
2894 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
2895 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
2896 p2p
.cfg_op_channel
= 1;
2897 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
2898 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2901 p2p
.op_reg_class
= 81;
2903 * Use random operation channel from (1, 6, 11) if no other
2904 * preference is indicated.
2906 os_get_random((u8
*) &r
, sizeof(r
));
2907 p2p
.op_channel
= 1 + (r
% 3) * 5;
2908 p2p
.cfg_op_channel
= 0;
2909 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
2910 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2912 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
2913 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
2914 p2p
.country
[2] = 0x04;
2916 os_memcpy(p2p
.country
, "XX\x04", 3);
2918 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
)) {
2919 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
2924 os_memcpy(p2p
.pri_dev_type
, wpa_s
->conf
->device_type
,
2927 p2p
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2928 os_memcpy(p2p
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
2929 p2p
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2931 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
2932 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
2934 p2p
.max_peers
= 100;
2936 if (wpa_s
->conf
->p2p_ssid_postfix
) {
2937 p2p
.ssid_postfix_len
=
2938 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
2939 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
2940 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
2941 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
2942 p2p
.ssid_postfix_len
);
2945 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
2947 p2p
.max_listen
= wpa_s
->max_remain_on_chan
;
2949 global
->p2p
= p2p_init(&p2p
);
2950 if (global
->p2p
== NULL
)
2952 global
->p2p_init_wpa_s
= wpa_s
;
2954 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
2955 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
2957 p2p_add_wps_vendor_extension(
2958 global
->p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
2966 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2967 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2969 * This function deinitialize per-interface P2P data.
2971 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
2973 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
2974 wpa_drv_probe_req_report(wpa_s
, 0);
2976 if (wpa_s
->go_params
) {
2977 /* Clear any stored provisioning info */
2978 p2p_clear_provisioning_info(
2980 wpa_s
->go_params
->peer_device_addr
);
2983 os_free(wpa_s
->go_params
);
2984 wpa_s
->go_params
= NULL
;
2985 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2986 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2987 wpa_s
->p2p_long_listen
= 0;
2988 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
2989 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
2990 wpas_p2p_remove_pending_group_interface(wpa_s
);
2992 /* TODO: remove group interface from the driver if this wpa_s instance
2993 * is on top of a P2P group interface */
2998 * wpas_p2p_deinit_global - Deinitialize global P2P module
2999 * @global: Pointer to global data from wpa_supplicant_init()
3001 * This function deinitializes the global (per device) P2P module.
3003 void wpas_p2p_deinit_global(struct wpa_global
*global
)
3005 struct wpa_supplicant
*wpa_s
, *tmp
;
3007 wpa_s
= global
->ifaces
;
3009 wpas_p2p_service_flush(wpa_s
);
3011 if (global
->p2p
== NULL
)
3014 /* Remove remaining P2P group interfaces */
3015 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
3016 wpa_s
= wpa_s
->next
;
3018 tmp
= global
->ifaces
;
3021 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
3026 /* Disconnect from the P2P group and deinit the interface */
3027 wpas_p2p_disconnect(tmp
);
3031 * Deinit GO data on any possibly remaining interface (if main
3032 * interface is used as GO).
3034 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3035 if (wpa_s
->ap_iface
)
3036 wpas_p2p_group_deinit(wpa_s
);
3039 p2p_deinit(global
->p2p
);
3041 global
->p2p_init_wpa_s
= NULL
;
3045 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
3047 if (wpa_s
->conf
->p2p_no_group_iface
)
3048 return 0; /* separate interface disabled per configuration */
3049 if (wpa_s
->drv_flags
&
3050 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
3051 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
3052 return 1; /* P2P group requires a new interface in every case
3054 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
3055 return 0; /* driver does not support concurrent operations */
3056 if (wpa_s
->global
->ifaces
->next
)
3057 return 1; /* more that one interface already in use */
3058 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
3059 return 1; /* this interface is already in use */
3064 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
3065 const u8
*peer_addr
,
3066 enum p2p_wps_method wps_method
,
3067 int go_intent
, const u8
*own_interface_addr
,
3068 unsigned int force_freq
, int persistent_group
,
3069 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
3071 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3072 persistent_group
= 2;
3074 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3075 return wpa_drv_p2p_connect(wpa_s
, peer_addr
, wps_method
,
3076 go_intent
, own_interface_addr
,
3077 force_freq
, persistent_group
);
3081 * Increase GO config timeout if HT40 is used since it takes some time
3082 * to scan channels for coex purposes before the BSS can be started.
3084 p2p_set_config_timeout(wpa_s
->global
->p2p
,
3085 wpa_s
->p2p_go_ht40
? 255 : 100, 20);
3087 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
3088 go_intent
, own_interface_addr
, force_freq
,
3089 persistent_group
, ssid
? ssid
->ssid
: NULL
,
3090 ssid
? ssid
->ssid_len
: 0,
3091 wpa_s
->p2p_pd_before_go_neg
, pref_freq
);
3095 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
3096 const u8
*peer_addr
,
3097 enum p2p_wps_method wps_method
,
3098 int go_intent
, const u8
*own_interface_addr
,
3099 unsigned int force_freq
, int persistent_group
,
3100 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
3102 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3103 persistent_group
= 2;
3105 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3108 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
3109 go_intent
, own_interface_addr
, force_freq
,
3110 persistent_group
, ssid
? ssid
->ssid
: NULL
,
3111 ssid
? ssid
->ssid_len
: 0, pref_freq
);
3115 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
3117 wpa_s
->p2p_join_scan_count
++;
3118 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
3119 wpa_s
->p2p_join_scan_count
);
3120 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
3121 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
3122 " for join operationg - stop join attempt",
3123 MAC2STR(wpa_s
->pending_join_iface_addr
));
3124 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3125 if (wpa_s
->p2p_auto_pd
) {
3126 wpa_s
->p2p_auto_pd
= 0;
3127 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
3128 " p2p_dev_addr=" MACSTR
" status=N/A",
3129 MAC2STR(wpa_s
->pending_join_dev_addr
));
3132 wpa_msg(wpa_s
->parent
, MSG_INFO
,
3133 P2P_EVENT_GROUP_FORMATION_FAILURE
);
3138 static int wpas_check_freq_conflict(struct wpa_supplicant
*wpa_s
, int freq
)
3140 struct wpa_supplicant
*iface
;
3144 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)
3147 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
3148 if (!wpas_p2p_create_iface(wpa_s
) && iface
== wpa_s
)
3150 if (iface
->current_ssid
== NULL
|| iface
->assoc_freq
== 0)
3152 if (iface
->current_ssid
->mode
== WPAS_MODE_AP
||
3153 iface
->current_ssid
->mode
== WPAS_MODE_P2P_GO
)
3154 shared_freq
= iface
->current_ssid
->frequency
;
3155 else if (wpa_drv_get_bssid(iface
, bssid
) == 0)
3156 shared_freq
= iface
->assoc_freq
;
3160 if (shared_freq
&& freq
!= shared_freq
) {
3161 wpa_printf(MSG_DEBUG
, "P2P: Frequency conflict - %s "
3162 "connected on %d MHz - new connection on "
3163 "%d MHz", iface
->ifname
, shared_freq
, freq
);
3168 shared_freq
= wpa_drv_shared_freq(wpa_s
);
3169 if (shared_freq
> 0 && shared_freq
!= freq
) {
3170 wpa_printf(MSG_DEBUG
, "P2P: Frequency conflict - shared "
3171 "virtual interface connected on %d MHz - new "
3172 "connection on %d MHz", shared_freq
, freq
);
3180 static int wpas_p2p_peer_go(struct wpa_supplicant
*wpa_s
,
3181 const u8
*peer_dev_addr
)
3183 struct wpa_bss
*bss
;
3186 bss
= wpa_bss_get_p2p_dev_addr(wpa_s
, peer_dev_addr
);
3189 if (bss
->last_update_idx
< wpa_s
->bss_update_idx
) {
3190 wpa_printf(MSG_DEBUG
, "P2P: Peer BSS entry not updated in the "
3195 updated
= os_time_before(&wpa_s
->p2p_auto_started
, &bss
->last_update
);
3196 wpa_printf(MSG_DEBUG
, "P2P: Current BSS entry for peer updated at "
3197 "%ld.%06ld (%supdated in last scan)",
3198 bss
->last_update
.sec
, bss
->last_update
.usec
,
3199 updated
? "": "not ");
3205 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
3206 struct wpa_scan_results
*scan_res
)
3208 struct wpa_bss
*bss
;
3210 u8 iface_addr
[ETH_ALEN
];
3212 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3214 if (wpa_s
->global
->p2p_disabled
)
3217 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for %sjoin",
3218 scan_res
? (int) scan_res
->num
: -1,
3219 wpa_s
->p2p_auto_join
? "auto_" : "");
3222 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
3224 if (wpa_s
->p2p_auto_pd
) {
3225 int join
= wpas_p2p_peer_go(wpa_s
,
3226 wpa_s
->pending_join_dev_addr
);
3228 wpa_s
->auto_pd_scan_retry
< P2P_AUTO_PD_SCAN_ATTEMPTS
) {
3229 wpa_s
->auto_pd_scan_retry
++;
3230 bss
= wpa_bss_get_bssid(wpa_s
,
3231 wpa_s
->pending_join_dev_addr
);
3234 wpa_printf(MSG_DEBUG
, "P2P: Scan retry %d for "
3235 "the peer " MACSTR
" at %d MHz",
3236 wpa_s
->auto_pd_scan_retry
,
3238 pending_join_dev_addr
),
3240 wpas_p2p_join_scan_req(wpa_s
, freq
);
3248 wpa_s
->p2p_auto_pd
= 0;
3249 wpa_s
->pending_pd_use
= join
? AUTO_PD_JOIN
: AUTO_PD_GO_NEG
;
3250 wpa_printf(MSG_DEBUG
, "P2P: Auto PD with " MACSTR
" join=%d",
3251 MAC2STR(wpa_s
->pending_join_dev_addr
), join
);
3252 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
3253 wpa_s
->pending_join_dev_addr
,
3254 wpa_s
->pending_pd_config_methods
, join
,
3255 0, wpa_s
->user_initiated_pd
) < 0) {
3256 wpa_s
->p2p_auto_pd
= 0;
3257 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
3258 " p2p_dev_addr=" MACSTR
" status=N/A",
3259 MAC2STR(wpa_s
->pending_join_dev_addr
));
3264 if (wpa_s
->p2p_auto_join
) {
3265 int join
= wpas_p2p_peer_go(wpa_s
,
3266 wpa_s
->pending_join_dev_addr
);
3268 wpa_printf(MSG_DEBUG
, "P2P: Peer was not found to be "
3269 "running a GO -> use GO Negotiation");
3270 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
,
3271 wpa_s
->p2p_pin
, wpa_s
->p2p_wps_method
,
3272 wpa_s
->p2p_persistent_group
, 0, 0, 0,
3273 wpa_s
->p2p_go_intent
,
3274 wpa_s
->p2p_connect_freq
,
3275 wpa_s
->p2p_persistent_id
,
3276 wpa_s
->p2p_pd_before_go_neg
,
3277 wpa_s
->p2p_go_ht40
);
3281 wpa_printf(MSG_DEBUG
, "P2P: Peer was found running GO%s -> "
3282 "try to join the group", join
? "" :
3285 wpa_s
->p2p_fallback_to_go_neg
= 1;
3288 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
3289 wpa_s
->pending_join_iface_addr
);
3291 p2p_get_interface_addr(wpa_s
->global
->p2p
,
3292 wpa_s
->pending_join_dev_addr
,
3294 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
3296 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
3297 "address for join from " MACSTR
" to " MACSTR
3298 " based on newly discovered P2P peer entry",
3299 MAC2STR(wpa_s
->pending_join_iface_addr
),
3300 MAC2STR(iface_addr
));
3301 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
3304 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
3305 wpa_s
->pending_join_iface_addr
);
3308 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
3309 "from P2P peer table: %d MHz", freq
);
3311 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
3314 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
3315 "from BSS table: %d MHz", freq
);
3320 if (wpas_check_freq_conflict(wpa_s
, freq
) > 0) {
3321 wpa_msg(wpa_s
->parent
, MSG_INFO
,
3322 P2P_EVENT_GROUP_FORMATION_FAILURE
3323 "reason=FREQ_CONFLICT");
3327 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
3328 "prior to joining an existing group (GO " MACSTR
3330 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
3331 wpa_s
->pending_pd_before_join
= 1;
3333 switch (wpa_s
->pending_join_wps_method
) {
3334 case WPS_PIN_DISPLAY
:
3335 method
= WPS_CONFIG_KEYPAD
;
3337 case WPS_PIN_KEYPAD
:
3338 method
= WPS_CONFIG_DISPLAY
;
3341 method
= WPS_CONFIG_PUSHBUTTON
;
3348 if ((p2p_get_provisioning_info(wpa_s
->global
->p2p
,
3349 wpa_s
->pending_join_dev_addr
) ==
3352 * We have already performed provision discovery for
3353 * joining the group. Proceed directly to join
3354 * operation without duplicated provision discovery. */
3355 wpa_printf(MSG_DEBUG
, "P2P: Provision discovery "
3356 "with " MACSTR
" already done - proceed to "
3358 MAC2STR(wpa_s
->pending_join_dev_addr
));
3359 wpa_s
->pending_pd_before_join
= 0;
3363 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
3364 wpa_s
->pending_join_dev_addr
, method
, 1,
3365 freq
, wpa_s
->user_initiated_pd
) < 0) {
3366 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
3367 "Discovery Request before joining an "
3369 wpa_s
->pending_pd_before_join
= 0;
3375 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
3376 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3377 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
3378 wpas_p2p_check_join_scan_limit(wpa_s
);
3382 /* Start join operation immediately */
3383 wpas_p2p_join_start(wpa_s
);
3387 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
)
3390 struct wpa_driver_scan_params params
;
3391 struct wpabuf
*wps_ie
, *ies
;
3393 int freqs
[2] = { 0, 0 };
3395 os_memset(¶ms
, 0, sizeof(params
));
3397 /* P2P Wildcard SSID */
3398 params
.num_ssids
= 1;
3399 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
3400 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
3402 wpa_s
->wps
->dev
.p2p
= 1;
3403 wps_ie
= wps_build_probe_req_ie(DEV_PW_DEFAULT
, &wpa_s
->wps
->dev
,
3404 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
, 0,
3406 if (wps_ie
== NULL
) {
3407 wpas_p2p_scan_res_join(wpa_s
, NULL
);
3411 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
3412 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
3414 wpabuf_free(wps_ie
);
3415 wpas_p2p_scan_res_join(wpa_s
, NULL
);
3418 wpabuf_put_buf(ies
, wps_ie
);
3419 wpabuf_free(wps_ie
);
3421 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
3423 params
.p2p_probe
= 1;
3424 params
.extra_ies
= wpabuf_head(ies
);
3425 params
.extra_ies_len
= wpabuf_len(ies
);
3428 params
.freqs
= freqs
;
3432 * Run a scan to update BSS table and start Provision Discovery once
3433 * the new scan results become available.
3435 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
3437 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
3442 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
3444 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3445 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
3446 wpas_p2p_check_join_scan_limit(wpa_s
);
3451 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
3453 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3454 wpas_p2p_join_scan_req(wpa_s
, 0);
3458 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
3459 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
3462 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
3463 MACSTR
" dev " MACSTR
")%s",
3464 MAC2STR(iface_addr
), MAC2STR(dev_addr
),
3465 auto_join
? " (auto_join)" : "");
3467 wpa_s
->p2p_auto_pd
= 0;
3468 wpa_s
->p2p_auto_join
= !!auto_join
;
3469 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
3470 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
3471 wpa_s
->pending_join_wps_method
= wps_method
;
3473 /* Make sure we are not running find during connection establishment */
3474 wpas_p2p_stop_find(wpa_s
);
3476 wpa_s
->p2p_join_scan_count
= 0;
3477 wpas_p2p_join_scan(wpa_s
, NULL
);
3482 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
3484 struct wpa_supplicant
*group
;
3485 struct p2p_go_neg_results res
;
3486 struct wpa_bss
*bss
;
3488 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
3491 if (group
!= wpa_s
) {
3492 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
3493 sizeof(group
->p2p_pin
));
3494 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
3497 group
->p2p_in_provisioning
= 1;
3498 wpa_s
->global
->p2p_group_formation
= wpa_s
;
3499 group
->p2p_fallback_to_go_neg
= wpa_s
->p2p_fallback_to_go_neg
;
3501 os_memset(&res
, 0, sizeof(res
));
3502 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
3504 res
.wps_method
= wpa_s
->pending_join_wps_method
;
3505 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
3507 res
.freq
= bss
->freq
;
3508 res
.ssid_len
= bss
->ssid_len
;
3509 os_memcpy(res
.ssid
, bss
->ssid
, bss
->ssid_len
);
3512 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
3513 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel prior to "
3515 wpa_drv_cancel_remain_on_channel(wpa_s
);
3516 wpa_s
->off_channel_freq
= 0;
3517 wpa_s
->roc_waiting_drv_freq
= 0;
3519 wpas_start_wps_enrollee(group
, &res
);
3522 * Allow a longer timeout for join-a-running-group than normal 15
3523 * second group formation timeout since the GO may not have authorized
3524 * our connection yet.
3526 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
3527 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
3535 * wpas_p2p_connect - Request P2P Group Formation to be started
3536 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3537 * @peer_addr: Address of the peer P2P Device
3538 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3539 * @persistent_group: Whether to create a persistent group
3540 * @auto_join: Whether to select join vs. GO Negotiation automatically
3541 * @join: Whether to join an existing group (as a client) instead of starting
3542 * Group Owner negotiation; @peer_addr is BSSID in that case
3543 * @auth: Whether to only authorize the connection instead of doing that and
3544 * initiating Group Owner negotiation
3545 * @go_intent: GO Intent or -1 to use default
3546 * @freq: Frequency for the group or 0 for auto-selection
3547 * @persistent_id: Persistent group credentials to use for forcing GO
3548 * parameters or -1 to generate new values (SSID/passphrase)
3549 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3550 * interoperability workaround when initiating group formation
3551 * @ht40: Start GO with 40 MHz channel width
3552 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3553 * failure, -2 on failure due to channel not currently available,
3554 * -3 if forced channel is not supported
3556 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3557 const char *pin
, enum p2p_wps_method wps_method
,
3558 int persistent_group
, int auto_join
, int join
, int auth
,
3559 int go_intent
, int freq
, int persistent_id
, int pd
,
3562 int force_freq
= 0, pref_freq
= 0, oper_freq
= 0;
3565 enum wpa_driver_if_type iftype
;
3567 struct wpa_ssid
*ssid
= NULL
;
3569 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3572 if (persistent_id
>= 0) {
3573 ssid
= wpa_config_get_network(wpa_s
->conf
, persistent_id
);
3574 if (ssid
== NULL
|| ssid
->disabled
!= 2 ||
3575 ssid
->mode
!= WPAS_MODE_P2P_GO
)
3580 go_intent
= wpa_s
->conf
->p2p_go_intent
;
3583 wpa_s
->p2p_long_listen
= 0;
3585 wpa_s
->p2p_wps_method
= wps_method
;
3586 wpa_s
->p2p_persistent_group
= !!persistent_group
;
3587 wpa_s
->p2p_persistent_id
= persistent_id
;
3588 wpa_s
->p2p_go_intent
= go_intent
;
3589 wpa_s
->p2p_connect_freq
= freq
;
3590 wpa_s
->p2p_fallback_to_go_neg
= 0;
3591 wpa_s
->p2p_pd_before_go_neg
= !!pd
;
3592 wpa_s
->p2p_go_ht40
= !!ht40
;
3595 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
3596 else if (wps_method
== WPS_PIN_DISPLAY
) {
3597 ret
= wps_generate_pin();
3598 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
3600 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
3603 wpa_s
->p2p_pin
[0] = '\0';
3605 if (join
|| auto_join
) {
3606 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
3608 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
3609 "connect a running group from " MACSTR
,
3610 MAC2STR(peer_addr
));
3611 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
3614 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
3615 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
3617 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
3618 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
3622 os_get_time(&wpa_s
->p2p_auto_started
);
3623 wpa_printf(MSG_DEBUG
, "P2P: Auto join started at "
3625 wpa_s
->p2p_auto_started
.sec
,
3626 wpa_s
->p2p_auto_started
.usec
);
3628 wpa_s
->user_initiated_pd
= 1;
3629 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
,
3635 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
3637 oper_freq
= wpa_s
->assoc_freq
;
3639 oper_freq
= wpa_drv_shared_freq(wpa_s
);
3645 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3646 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
3647 "(%u MHz) is not supported for P2P uses",
3652 if (oper_freq
> 0 && freq
!= oper_freq
&&
3653 !(wpa_s
->drv_flags
&
3654 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
3655 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
3656 "on %u MHz while connected on another "
3657 "channel (%u MHz)", freq
, oper_freq
);
3660 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
3661 "requested channel (%u MHz)", freq
);
3663 } else if (oper_freq
> 0 &&
3664 !p2p_supported_freq(wpa_s
->global
->p2p
, oper_freq
)) {
3665 if (!(wpa_s
->drv_flags
&
3666 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
3667 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
3668 "while connected on non-P2P supported "
3669 "channel (%u MHz)", oper_freq
);
3672 wpa_printf(MSG_DEBUG
, "P2P: Current operating channel "
3673 "(%u MHz) not available for P2P - try to use "
3674 "another channel", oper_freq
);
3676 } else if (oper_freq
> 0 &&
3678 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
3679 wpa_printf(MSG_DEBUG
, "P2P: Trying to prefer the channel we "
3680 "are already using (%u MHz) on another interface",
3682 pref_freq
= oper_freq
;
3683 } else if (oper_freq
> 0) {
3684 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
3685 "channel we are already using (%u MHz) on another "
3686 "interface", oper_freq
);
3687 force_freq
= oper_freq
;
3690 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
3692 if (wpa_s
->create_p2p_iface
) {
3693 /* Prepare to add a new interface for the group */
3694 iftype
= WPA_IF_P2P_GROUP
;
3695 if (go_intent
== 15)
3696 iftype
= WPA_IF_P2P_GO
;
3697 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
3698 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
3699 "interface for the group");
3703 if_addr
= wpa_s
->pending_interface_addr
;
3705 if_addr
= wpa_s
->own_addr
;
3708 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
3710 force_freq
, persistent_group
, ssid
,
3716 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
3717 go_intent
, if_addr
, force_freq
,
3718 persistent_group
, ssid
, pref_freq
) < 0) {
3719 if (wpa_s
->create_p2p_iface
)
3720 wpas_p2p_remove_pending_group_interface(wpa_s
);
3728 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3729 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3730 * @freq: Frequency of the channel in MHz
3731 * @duration: Duration of the stay on the channel in milliseconds
3733 * This callback is called when the driver indicates that it has started the
3734 * requested remain-on-channel duration.
3736 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
3737 unsigned int freq
, unsigned int duration
)
3739 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3741 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
3742 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
3743 wpa_s
->pending_listen_duration
);
3744 wpa_s
->pending_listen_freq
= 0;
3749 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
3750 unsigned int timeout
)
3752 /* Limit maximum Listen state time based on driver limitation. */
3753 if (timeout
> wpa_s
->max_remain_on_chan
)
3754 timeout
= wpa_s
->max_remain_on_chan
;
3756 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3757 return wpa_drv_p2p_listen(wpa_s
, timeout
);
3759 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
3764 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3765 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3766 * @freq: Frequency of the channel in MHz
3768 * This callback is called when the driver indicates that a remain-on-channel
3769 * operation has been completed, i.e., the duration on the requested channel
3772 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
3775 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
3776 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3777 wpa_s
->p2p_long_listen
, offchannel_pending_action_tx(wpa_s
));
3778 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3780 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
3781 return; /* P2P module started a new operation */
3782 if (offchannel_pending_action_tx(wpa_s
))
3784 if (wpa_s
->p2p_long_listen
> 0)
3785 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
3786 if (wpa_s
->p2p_long_listen
> 0) {
3787 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
3788 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
3794 * wpas_p2p_group_remove - Remove a P2P group
3795 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3796 * @ifname: Network interface name of the group interface or "*" to remove all
3798 * Returns: 0 on success, -1 on failure
3800 * This function is used to remove a P2P group. This can be used to disconnect
3801 * from a group in which the local end is a P2P Client or to end a P2P Group in
3802 * case the local end is the Group Owner. If a virtual network interface was
3803 * created for this group, that interface will be removed. Otherwise, only the
3804 * configured P2P group network will be removed from the interface.
3806 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
3808 struct wpa_global
*global
= wpa_s
->global
;
3810 if (os_strcmp(ifname
, "*") == 0) {
3811 struct wpa_supplicant
*prev
;
3812 wpa_s
= global
->ifaces
;
3815 wpa_s
= wpa_s
->next
;
3816 wpas_p2p_disconnect(prev
);
3821 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3822 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3826 return wpas_p2p_disconnect(wpa_s
);
3830 static int wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
3831 struct p2p_go_neg_results
*params
,
3837 os_memset(params
, 0, sizeof(*params
));
3838 params
->role_go
= 1;
3839 params
->ht40
= ht40
;
3841 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
3842 "frequency %d MHz", freq
);
3843 params
->freq
= freq
;
3844 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
3845 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
3846 wpa_s
->conf
->p2p_oper_channel
<= 11) {
3847 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3848 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3849 "frequency %d MHz", params
->freq
);
3850 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
3851 wpa_s
->conf
->p2p_oper_reg_class
== 124) {
3852 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3853 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3854 "frequency %d MHz", params
->freq
);
3855 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3856 wpa_s
->best_overall_freq
> 0 &&
3857 p2p_supported_freq(wpa_s
->global
->p2p
,
3858 wpa_s
->best_overall_freq
)) {
3859 params
->freq
= wpa_s
->best_overall_freq
;
3860 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
3861 "channel %d MHz", params
->freq
);
3862 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3863 wpa_s
->best_24_freq
> 0 &&
3864 p2p_supported_freq(wpa_s
->global
->p2p
,
3865 wpa_s
->best_24_freq
)) {
3866 params
->freq
= wpa_s
->best_24_freq
;
3867 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
3868 "channel %d MHz", params
->freq
);
3869 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3870 wpa_s
->best_5_freq
> 0 &&
3871 p2p_supported_freq(wpa_s
->global
->p2p
,
3872 wpa_s
->best_5_freq
)) {
3873 params
->freq
= wpa_s
->best_5_freq
;
3874 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
3875 "channel %d MHz", params
->freq
);
3878 for (chan
= 0; chan
< 11; chan
++) {
3879 params
->freq
= 2412 + chan
* 5;
3880 if (!wpas_p2p_disallowed_freq(wpa_s
->global
,
3885 wpa_printf(MSG_DEBUG
, "P2P: No 2.4 GHz channel "
3889 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
3890 "known)", params
->freq
);
3893 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
3894 wpa_s
->assoc_freq
&& !freq
) {
3895 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3897 params
->freq
= wpa_s
->assoc_freq
;
3900 res
= wpa_drv_shared_freq(wpa_s
);
3901 if (res
> 0 && !freq
) {
3902 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3903 "already using on a shared interface");
3905 } else if (res
> 0 && freq
!= res
&&
3906 !(wpa_s
->drv_flags
&
3907 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
3908 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group on %u MHz "
3909 "while connected on another channel (%u MHz)",
3918 static struct wpa_supplicant
*
3919 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
3922 struct wpa_supplicant
*group_wpa_s
;
3924 if (!wpas_p2p_create_iface(wpa_s
)) {
3925 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use same interface for group "
3930 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
3931 WPA_IF_P2P_CLIENT
) < 0) {
3932 wpa_msg(wpa_s
, MSG_ERROR
, "P2P: Failed to add group interface");
3935 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
3936 if (group_wpa_s
== NULL
) {
3937 wpa_msg(wpa_s
, MSG_ERROR
, "P2P: Failed to initialize group "
3939 wpas_p2p_remove_pending_group_interface(wpa_s
);
3943 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use separate group interface %s",
3944 group_wpa_s
->ifname
);
3950 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3951 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3952 * @persistent_group: Whether to create a persistent group
3953 * @freq: Frequency for the group or 0 to indicate no hardcoding
3954 * Returns: 0 on success, -1 on failure
3956 * This function creates a new P2P group with the local end as the Group Owner,
3957 * i.e., without using Group Owner Negotiation.
3959 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
3962 struct p2p_go_neg_results params
;
3965 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3968 /* Make sure we are not running find during connection establishment */
3969 wpa_printf(MSG_DEBUG
, "P2P: Stop any on-going P2P FIND");
3970 wpas_p2p_stop_find_oper(wpa_s
);
3973 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
3975 if (wpa_s
->best_24_freq
> 0 &&
3976 p2p_supported_freq(wpa_s
->global
->p2p
,
3977 wpa_s
->best_24_freq
)) {
3978 freq
= wpa_s
->best_24_freq
;
3979 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
3980 "channel: %d MHz", freq
);
3982 os_get_random((u8
*) &r
, sizeof(r
));
3983 freq
= 2412 + (r
% 3) * 25;
3984 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
3985 "channel: %d MHz", freq
);
3990 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
3992 if (wpa_s
->best_5_freq
> 0 &&
3993 p2p_supported_freq(wpa_s
->global
->p2p
,
3994 wpa_s
->best_5_freq
)) {
3995 freq
= wpa_s
->best_5_freq
;
3996 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
3997 "channel: %d MHz", freq
);
3999 os_get_random((u8
*) &r
, sizeof(r
));
4000 freq
= 5180 + (r
% 4) * 20;
4001 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
4002 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
4003 "5 GHz channel for P2P group");
4006 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
4007 "channel: %d MHz", freq
);
4011 if (freq
> 0 && !p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
4012 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
4013 "(%u MHz) is not supported for P2P uses",
4018 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
))
4021 !p2p_supported_freq(wpa_s
->global
->p2p
, params
.freq
)) {
4022 wpa_printf(MSG_DEBUG
, "P2P: The selected channel for GO "
4023 "(%u MHz) is not supported for P2P uses",
4027 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
4028 params
.persistent_group
= persistent_group
;
4030 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
4033 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
4039 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
4040 struct wpa_ssid
*params
, int addr_allocated
)
4042 struct wpa_ssid
*ssid
;
4044 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
4048 wpa_supplicant_ap_deinit(wpa_s
);
4050 ssid
= wpa_config_add_network(wpa_s
->conf
);
4053 wpa_config_set_network_defaults(ssid
);
4054 ssid
->temporary
= 1;
4055 ssid
->proto
= WPA_PROTO_RSN
;
4056 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
4057 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
4058 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
4059 ssid
->ssid
= os_malloc(params
->ssid_len
);
4060 if (ssid
->ssid
== NULL
) {
4061 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
4064 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
4065 ssid
->ssid_len
= params
->ssid_len
;
4066 ssid
->p2p_group
= 1;
4067 ssid
->export_keys
= 1;
4068 if (params
->psk_set
) {
4069 os_memcpy(ssid
->psk
, params
->psk
, 32);
4072 if (params
->passphrase
)
4073 ssid
->passphrase
= os_strdup(params
->passphrase
);
4075 wpa_supplicant_select_network(wpa_s
, ssid
);
4077 wpa_s
->show_group_started
= 1;
4083 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
4084 struct wpa_ssid
*ssid
, int addr_allocated
,
4087 struct p2p_go_neg_results params
;
4090 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
4093 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
4094 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
4095 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
4100 /* Make sure we are not running find during connection establishment */
4101 wpas_p2p_stop_find_oper(wpa_s
);
4103 wpa_s
->p2p_fallback_to_go_neg
= 0;
4105 if (ssid
->mode
== WPAS_MODE_INFRA
)
4106 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
4108 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
4111 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
))
4115 params
.psk_set
= ssid
->psk_set
;
4117 os_memcpy(params
.psk
, ssid
->psk
, sizeof(params
.psk
));
4118 if (ssid
->passphrase
) {
4119 if (os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
4120 wpa_printf(MSG_ERROR
, "P2P: Invalid passphrase in "
4121 "persistent group");
4124 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
4125 sizeof(params
.passphrase
));
4127 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
4128 params
.ssid_len
= ssid
->ssid_len
;
4129 params
.persistent_group
= 1;
4131 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
4135 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
4141 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
4142 struct wpabuf
*proberesp_ies
)
4144 struct wpa_supplicant
*wpa_s
= ctx
;
4145 if (wpa_s
->ap_iface
) {
4146 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
4147 if (!(hapd
->conf
->p2p
& P2P_GROUP_OWNER
)) {
4148 wpabuf_free(beacon_ies
);
4149 wpabuf_free(proberesp_ies
);
4153 wpabuf_free(hapd
->p2p_beacon_ie
);
4154 hapd
->p2p_beacon_ie
= beacon_ies
;
4156 wpabuf_free(hapd
->p2p_probe_resp_ie
);
4157 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
4159 wpabuf_free(beacon_ies
);
4160 wpabuf_free(proberesp_ies
);
4162 wpa_supplicant_ap_update_beacon(wpa_s
);
4166 static void wpas_p2p_idle_update(void *ctx
, int idle
)
4168 struct wpa_supplicant
*wpa_s
= ctx
;
4169 if (!wpa_s
->ap_iface
)
4171 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
4173 wpas_p2p_set_group_idle_timeout(wpa_s
);
4175 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
4179 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
4180 struct wpa_ssid
*ssid
)
4182 struct p2p_group
*group
;
4183 struct p2p_group_config
*cfg
;
4185 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4187 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4190 cfg
= os_zalloc(sizeof(*cfg
));
4194 if (ssid
->p2p_persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
4195 cfg
->persistent_group
= 2;
4196 else if (ssid
->p2p_persistent_group
)
4197 cfg
->persistent_group
= 1;
4198 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
4199 if (wpa_s
->max_stations
&&
4200 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
4201 cfg
->max_clients
= wpa_s
->max_stations
;
4203 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
4204 os_memcpy(cfg
->ssid
, ssid
->ssid
, ssid
->ssid_len
);
4205 cfg
->ssid_len
= ssid
->ssid_len
;
4206 cfg
->cb_ctx
= wpa_s
;
4207 cfg
->ie_update
= wpas_p2p_ie_update
;
4208 cfg
->idle_update
= wpas_p2p_idle_update
;
4210 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
4213 if (ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
)
4214 p2p_group_notif_formation_done(group
);
4215 wpa_s
->p2p_group
= group
;
4220 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4223 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
4225 if (!wpa_s
->p2p_in_provisioning
) {
4226 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
4227 "provisioning not in progress");
4231 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
4232 u8 go_dev_addr
[ETH_ALEN
];
4233 os_memcpy(go_dev_addr
, wpa_s
->bssid
, ETH_ALEN
);
4234 wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
4236 /* Clear any stored provisioning info */
4237 p2p_clear_provisioning_info(wpa_s
->global
->p2p
, go_dev_addr
);
4240 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
4242 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
4244 * Use a separate timeout for initial data connection to
4245 * complete to allow the group to be removed automatically if
4246 * something goes wrong in this step before the P2P group idle
4247 * timeout mechanism is taken into use.
4249 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT
, 0,
4250 wpas_p2p_group_formation_timeout
,
4251 wpa_s
->parent
, NULL
);
4253 if (wpa_s
->global
->p2p
)
4254 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
4255 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4256 wpa_drv_wps_success_cb(wpa_s
, peer_addr
);
4257 wpas_group_formation_completed(wpa_s
, 1);
4261 void wpas_p2p_wps_failed(struct wpa_supplicant
*wpa_s
,
4262 struct wps_event_fail
*fail
)
4264 if (!wpa_s
->p2p_in_provisioning
) {
4265 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS fail event - P2P "
4266 "provisioning not in progress");
4270 if (wpa_s
->go_params
) {
4271 p2p_clear_provisioning_info(
4273 wpa_s
->go_params
->peer_device_addr
);
4276 wpas_notify_p2p_wps_failed(wpa_s
, fail
);
4280 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4281 const char *config_method
,
4282 enum wpas_p2p_prov_disc_use use
)
4286 wpa_s
->p2p_fallback_to_go_neg
= 0;
4287 wpa_s
->pending_pd_use
= NORMAL_PD
;
4288 if (os_strncmp(config_method
, "display", 7) == 0)
4289 config_methods
= WPS_CONFIG_DISPLAY
;
4290 else if (os_strncmp(config_method
, "keypad", 6) == 0)
4291 config_methods
= WPS_CONFIG_KEYPAD
;
4292 else if (os_strncmp(config_method
, "pbc", 3) == 0 ||
4293 os_strncmp(config_method
, "pushbutton", 10) == 0)
4294 config_methods
= WPS_CONFIG_PUSHBUTTON
;
4296 wpa_printf(MSG_DEBUG
, "P2P: Unknown config method");
4300 if (use
== WPAS_P2P_PD_AUTO
) {
4301 os_memcpy(wpa_s
->pending_join_dev_addr
, peer_addr
, ETH_ALEN
);
4302 wpa_s
->pending_pd_config_methods
= config_methods
;
4303 wpa_s
->p2p_auto_pd
= 1;
4304 wpa_s
->p2p_auto_join
= 0;
4305 wpa_s
->pending_pd_before_join
= 0;
4306 wpa_s
->auto_pd_scan_retry
= 0;
4307 wpas_p2p_stop_find(wpa_s
);
4308 wpa_s
->p2p_join_scan_count
= 0;
4309 os_get_time(&wpa_s
->p2p_auto_started
);
4310 wpa_printf(MSG_DEBUG
, "P2P: Auto PD started at %ld.%06ld",
4311 wpa_s
->p2p_auto_started
.sec
,
4312 wpa_s
->p2p_auto_started
.usec
);
4313 wpas_p2p_join_scan(wpa_s
, NULL
);
4317 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
4318 return wpa_drv_p2p_prov_disc_req(wpa_s
, peer_addr
,
4320 use
== WPAS_P2P_PD_FOR_JOIN
);
4323 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
4326 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
4327 config_methods
, use
== WPAS_P2P_PD_FOR_JOIN
,
4332 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
4335 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
4339 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
4341 if (!offchannel_pending_action_tx(wpa_s
))
4344 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
4345 "operation request");
4346 offchannel_clear_pending_action_tx(wpa_s
);
4350 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
4351 enum p2p_discovery_type type
,
4352 unsigned int num_req_dev_types
, const u8
*req_dev_types
,
4353 const u8
*dev_id
, unsigned int search_delay
)
4355 wpas_p2p_clear_pending_action_tx(wpa_s
);
4356 wpa_s
->p2p_long_listen
= 0;
4358 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4359 return wpa_drv_p2p_find(wpa_s
, timeout
, type
);
4361 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
||
4362 wpa_s
->p2p_in_provisioning
)
4365 wpa_supplicant_cancel_sched_scan(wpa_s
);
4367 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
,
4368 num_req_dev_types
, req_dev_types
, dev_id
,
4373 static int wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
)
4375 wpas_p2p_clear_pending_action_tx(wpa_s
);
4376 wpa_s
->p2p_long_listen
= 0;
4377 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
4378 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
4379 wpa_s
->global
->p2p_cb_on_scan_complete
= 0;
4381 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
4382 wpa_drv_p2p_stop_find(wpa_s
);
4386 if (wpa_s
->global
->p2p
)
4387 p2p_stop_find(wpa_s
->global
->p2p
);
4393 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
4395 if (wpas_p2p_stop_find_oper(wpa_s
) > 0)
4397 wpas_p2p_remove_pending_group_interface(wpa_s
);
4401 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
4403 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
4404 wpa_s
->p2p_long_listen
= 0;
4408 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
4412 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4415 wpa_supplicant_cancel_sched_scan(wpa_s
);
4416 wpas_p2p_clear_pending_action_tx(wpa_s
);
4420 * This is a request for unlimited Listen state. However, at
4421 * least for now, this is mapped to a Listen state for one
4426 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
4427 wpa_s
->p2p_long_listen
= 0;
4430 * Stop previous find/listen operation to avoid trying to request a new
4431 * remain-on-channel operation while the driver is still running the
4434 if (wpa_s
->global
->p2p
)
4435 p2p_stop_find(wpa_s
->global
->p2p
);
4437 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
4438 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
4439 wpa_s
->p2p_long_listen
= timeout
* 1000;
4440 eloop_register_timeout(timeout
, 0,
4441 wpas_p2p_long_listen_timeout
,
4449 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
4450 u8
*buf
, size_t len
, int p2p_group
)
4452 struct wpabuf
*p2p_ie
;
4455 if (wpa_s
->global
->p2p_disabled
)
4457 if (wpa_s
->global
->p2p
== NULL
)
4462 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
4463 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
4465 wpabuf_free(p2p_ie
);
4471 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
4472 const u8
*dst
, const u8
*bssid
,
4473 const u8
*ie
, size_t ie_len
, int ssi_signal
)
4475 if (wpa_s
->global
->p2p_disabled
)
4477 if (wpa_s
->global
->p2p
== NULL
)
4480 switch (p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, dst
, bssid
,
4482 case P2P_PREQ_NOT_P2P
:
4483 wpas_notify_preq(wpa_s
, addr
, dst
, bssid
, ie
, ie_len
,
4486 case P2P_PREQ_MALFORMED
:
4487 case P2P_PREQ_NOT_LISTEN
:
4488 case P2P_PREQ_NOT_PROCESSED
:
4489 default: /* make gcc happy */
4491 case P2P_PREQ_PROCESSED
:
4497 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
4498 const u8
*sa
, const u8
*bssid
,
4499 u8 category
, const u8
*data
, size_t len
, int freq
)
4501 if (wpa_s
->global
->p2p_disabled
)
4503 if (wpa_s
->global
->p2p
== NULL
)
4506 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
4511 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
4513 if (wpa_s
->global
->p2p_disabled
)
4515 if (wpa_s
->global
->p2p
== NULL
)
4518 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
4522 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
4524 p2p_group_deinit(wpa_s
->p2p_group
);
4525 wpa_s
->p2p_group
= NULL
;
4527 wpa_s
->ap_configured_cb
= NULL
;
4528 wpa_s
->ap_configured_cb_ctx
= NULL
;
4529 wpa_s
->ap_configured_cb_data
= NULL
;
4530 wpa_s
->connect_without_scan
= NULL
;
4534 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
4536 wpa_s
->p2p_long_listen
= 0;
4538 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4539 return wpa_drv_p2p_reject(wpa_s
, addr
);
4541 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4544 return p2p_reject(wpa_s
->global
->p2p
, addr
);
4548 /* Invite to reinvoke a persistent group */
4549 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4550 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
, int freq
,
4553 enum p2p_invite_role role
;
4556 wpa_s
->p2p_persistent_go_freq
= freq
;
4557 wpa_s
->p2p_go_ht40
= !!ht40
;
4558 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
4559 role
= P2P_INVITE_ROLE_GO
;
4560 if (peer_addr
== NULL
) {
4561 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
4562 "address in invitation command");
4565 if (wpas_p2p_create_iface(wpa_s
)) {
4566 if (wpas_p2p_add_group_interface(wpa_s
,
4567 WPA_IF_P2P_GO
) < 0) {
4568 wpa_printf(MSG_ERROR
, "P2P: Failed to "
4569 "allocate a new interface for the "
4573 bssid
= wpa_s
->pending_interface_addr
;
4575 bssid
= wpa_s
->own_addr
;
4577 role
= P2P_INVITE_ROLE_CLIENT
;
4578 peer_addr
= ssid
->bssid
;
4580 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
4582 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4583 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
4584 ssid
->ssid
, ssid
->ssid_len
,
4587 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4590 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
4591 ssid
->ssid
, ssid
->ssid_len
, freq
, go_dev_addr
, 1);
4595 /* Invite to join an active group */
4596 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
4597 const u8
*peer_addr
, const u8
*go_dev_addr
)
4599 struct wpa_global
*global
= wpa_s
->global
;
4600 enum p2p_invite_role role
;
4602 struct wpa_ssid
*ssid
;
4605 wpa_s
->p2p_persistent_go_freq
= 0;
4606 wpa_s
->p2p_go_ht40
= 0;
4608 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4609 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
4612 if (wpa_s
== NULL
) {
4613 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
4617 ssid
= wpa_s
->current_ssid
;
4619 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
4624 persistent
= ssid
->p2p_persistent_group
&&
4625 wpas_p2p_get_persistent(wpa_s
->parent
, peer_addr
,
4626 ssid
->ssid
, ssid
->ssid_len
);
4628 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
4629 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
4630 bssid
= wpa_s
->own_addr
;
4631 if (go_dev_addr
== NULL
)
4632 go_dev_addr
= wpa_s
->global
->p2p_dev_addr
;
4634 role
= P2P_INVITE_ROLE_CLIENT
;
4635 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
4636 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
4637 "invite to current group");
4640 bssid
= wpa_s
->bssid
;
4641 if (go_dev_addr
== NULL
&&
4642 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
4643 go_dev_addr
= wpa_s
->go_dev_addr
;
4645 wpa_s
->parent
->pending_invite_ssid_id
= -1;
4647 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4648 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
4649 ssid
->ssid
, ssid
->ssid_len
,
4650 go_dev_addr
, persistent
);
4652 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4655 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
4656 ssid
->ssid
, ssid
->ssid_len
, wpa_s
->assoc_freq
,
4657 go_dev_addr
, persistent
);
4661 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
4663 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
4664 const char *ssid_txt
;
4665 u8 go_dev_addr
[ETH_ALEN
];
4666 int network_id
= -1;
4670 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
) {
4671 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4672 wpa_s
->parent
, NULL
);
4675 if (!wpa_s
->show_group_started
|| !ssid
)
4678 wpa_s
->show_group_started
= 0;
4680 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
4681 os_memset(go_dev_addr
, 0, ETH_ALEN
);
4682 if (ssid
->bssid_set
)
4683 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
4684 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
4686 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
4688 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
4689 wpa_s
->global
->p2p_group_formation
= NULL
;
4691 freq
= wpa_s
->current_bss
? wpa_s
->current_bss
->freq
:
4692 (int) wpa_s
->assoc_freq
;
4693 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
4695 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
4696 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
4697 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4699 wpa_s
->ifname
, ssid_txt
, freq
, psk
,
4700 MAC2STR(go_dev_addr
),
4701 persistent
? " [PERSISTENT]" : "");
4703 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
4704 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4705 "go_dev_addr=" MACSTR
"%s",
4706 wpa_s
->ifname
, ssid_txt
, freq
,
4707 ssid
->passphrase
? ssid
->passphrase
: "",
4708 MAC2STR(go_dev_addr
),
4709 persistent
? " [PERSISTENT]" : "");
4713 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
4716 network_id
= ssid
->id
;
4717 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 1);
4720 if (wpa_s
->global
->p2p_cb_on_scan_complete
&& !wpa_s
->global
->p2p_disabled
&&
4721 wpa_s
->global
->p2p
!= NULL
) {
4722 wpa_s
->global
->p2p_cb_on_scan_complete
= 0;
4723 if (p2p_other_scan_completed(wpa_s
->global
->p2p
) == 1) {
4724 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Pending P2P operation "
4725 "continued after successful connection");
4726 p2p_increase_search_delay(
4728 wpas_p2p_search_delay(wpa_s
));
4734 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
4735 u32 interval1
, u32 duration2
, u32 interval2
)
4737 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4739 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4742 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
4743 wpa_s
->current_ssid
== NULL
||
4744 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
4747 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
4748 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
4749 duration1
, interval1
, duration2
, interval2
);
4753 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
4754 unsigned int interval
)
4756 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4759 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4762 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
4766 static int wpas_p2p_is_client(struct wpa_supplicant
*wpa_s
)
4768 if (wpa_s
->current_ssid
== NULL
) {
4770 * current_ssid can be cleared when P2P client interface gets
4771 * disconnected, so assume this interface was used as P2P
4776 return wpa_s
->current_ssid
->p2p_group
&&
4777 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
;
4781 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
4783 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
4785 if (wpa_s
->conf
->p2p_group_idle
== 0 && !wpas_p2p_is_client(wpa_s
)) {
4786 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
4791 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
4793 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_IDLE_TIMEOUT
);
4797 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
4801 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
4802 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
4804 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
4807 timeout
= wpa_s
->conf
->p2p_group_idle
;
4808 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
&&
4809 (timeout
== 0 || timeout
> P2P_MAX_CLIENT_IDLE
))
4810 timeout
= P2P_MAX_CLIENT_IDLE
;
4816 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
)
4817 timeout
= 0; /* special client mode no-timeout */
4822 if (wpa_s
->p2p_in_provisioning
) {
4824 * Use the normal group formation timeout during the
4825 * provisioning phase to avoid terminating this process too
4826 * early due to group idle timeout.
4828 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
4829 "during provisioning");
4833 if (wpa_s
->show_group_started
) {
4835 * Use the normal group formation timeout between the end of
4836 * the provisioning phase and completion of 4-way handshake to
4837 * avoid terminating this process too early due to group idle
4840 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
4841 "while waiting for initial 4-way handshake to "
4846 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
4848 eloop_register_timeout(timeout
, 0, wpas_p2p_group_idle_timeout
,
4853 /* Returns 1 if the interface was removed */
4854 int wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
4855 u16 reason_code
, const u8
*ie
, size_t ie_len
,
4856 int locally_generated
)
4858 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4860 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4863 if (!locally_generated
)
4864 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
4867 if (reason_code
== WLAN_REASON_DEAUTH_LEAVING
&& !locally_generated
&&
4868 wpa_s
->current_ssid
&&
4869 wpa_s
->current_ssid
->p2p_group
&&
4870 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
) {
4871 wpa_printf(MSG_DEBUG
, "P2P: GO indicated that the P2P Group "
4872 "session is ending");
4873 if (wpas_p2p_group_delete(wpa_s
,
4874 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
)
4883 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
4884 u16 reason_code
, const u8
*ie
, size_t ie_len
,
4885 int locally_generated
)
4887 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4889 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4892 if (!locally_generated
)
4893 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
4898 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
4900 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4905 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
4908 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
4909 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
4911 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
)
4912 p2p_set_pri_dev_type(p2p
, wpa_s
->conf
->device_type
);
4915 (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_CONFIG_METHODS
))
4916 p2p_set_config_methods(p2p
, wpa_s
->wps
->config_methods
);
4918 if (wpa_s
->wps
&& (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_UUID
))
4919 p2p_set_uuid(p2p
, wpa_s
->wps
->uuid
);
4921 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_WPS_STRING
) {
4922 p2p_set_manufacturer(p2p
, wpa_s
->conf
->manufacturer
);
4923 p2p_set_model_name(p2p
, wpa_s
->conf
->model_name
);
4924 p2p_set_model_number(p2p
, wpa_s
->conf
->model_number
);
4925 p2p_set_serial_number(p2p
, wpa_s
->conf
->serial_number
);
4928 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
)
4929 p2p_set_sec_dev_types(p2p
,
4930 (void *) wpa_s
->conf
->sec_device_type
,
4931 wpa_s
->conf
->num_sec_device_types
);
4933 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_VENDOR_EXTENSION
) {
4935 p2p_remove_wps_vendor_extensions(p2p
);
4936 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
4937 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
4939 p2p_add_wps_vendor_extension(
4940 p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
4944 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
4945 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
4947 country
[0] = wpa_s
->conf
->country
[0];
4948 country
[1] = wpa_s
->conf
->country
[1];
4950 p2p_set_country(p2p
, country
);
4953 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
4954 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
4955 wpa_s
->conf
->p2p_ssid_postfix
?
4956 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
4960 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
4961 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
4963 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_LISTEN_CHANNEL
) {
4964 u8 reg_class
, channel
;
4967 if (wpa_s
->conf
->p2p_listen_reg_class
&&
4968 wpa_s
->conf
->p2p_listen_channel
) {
4969 reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
4970 channel
= wpa_s
->conf
->p2p_listen_channel
;
4974 * Pick one of the social channels randomly as the
4977 os_get_random((u8
*) &r
, sizeof(r
));
4978 channel
= 1 + (r
% 3) * 5;
4980 ret
= p2p_set_listen_channel(p2p
, reg_class
, channel
);
4982 wpa_printf(MSG_ERROR
, "P2P: Own listen channel update "
4985 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_OPER_CHANNEL
) {
4986 u8 op_reg_class
, op_channel
, cfg_op_channel
;
4989 if (wpa_s
->conf
->p2p_oper_reg_class
&&
4990 wpa_s
->conf
->p2p_oper_channel
) {
4991 op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
4992 op_channel
= wpa_s
->conf
->p2p_oper_channel
;
4997 * Use random operation channel from (1, 6, 11)
4998 *if no other preference is indicated.
5000 os_get_random((u8
*) &r
, sizeof(r
));
5001 op_channel
= 1 + (r
% 3) * 5;
5004 ret
= p2p_set_oper_channel(p2p
, op_reg_class
, op_channel
,
5007 wpa_printf(MSG_ERROR
, "P2P: Own oper channel update "
5011 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_PREF_CHAN
) {
5012 if (p2p_set_pref_chan(p2p
, wpa_s
->conf
->num_p2p_pref_chan
,
5013 wpa_s
->conf
->p2p_pref_chan
) < 0) {
5014 wpa_printf(MSG_ERROR
, "P2P: Preferred channel list "
5021 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
5024 if (!wpa_s
->ap_iface
)
5026 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
5031 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
5033 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5035 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5038 wpa_s
->global
->cross_connection
= enabled
;
5039 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
5042 struct wpa_supplicant
*iface
;
5044 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
5046 if (iface
->cross_connect_enabled
== 0)
5049 iface
->cross_connect_enabled
= 0;
5050 iface
->cross_connect_in_use
= 0;
5051 wpa_msg(iface
->parent
, MSG_INFO
,
5052 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
5053 iface
->ifname
, iface
->cross_connect_uplink
);
5061 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
5063 struct wpa_supplicant
*iface
;
5065 if (!uplink
->global
->cross_connection
)
5068 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
5069 if (!iface
->cross_connect_enabled
)
5071 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
5074 if (iface
->ap_iface
== NULL
)
5076 if (iface
->cross_connect_in_use
)
5079 iface
->cross_connect_in_use
= 1;
5080 wpa_msg(iface
->parent
, MSG_INFO
,
5081 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
5082 iface
->ifname
, iface
->cross_connect_uplink
);
5087 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
5089 struct wpa_supplicant
*iface
;
5091 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
5092 if (!iface
->cross_connect_enabled
)
5094 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
5097 if (!iface
->cross_connect_in_use
)
5100 wpa_msg(iface
->parent
, MSG_INFO
,
5101 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
5102 iface
->ifname
, iface
->cross_connect_uplink
);
5103 iface
->cross_connect_in_use
= 0;
5108 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
5110 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
5111 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
5112 wpa_s
->cross_connect_disallowed
)
5113 wpas_p2p_disable_cross_connect(wpa_s
);
5115 wpas_p2p_enable_cross_connect(wpa_s
);
5116 if (!wpa_s
->ap_iface
&&
5117 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
5118 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
5122 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
5124 wpas_p2p_disable_cross_connect(wpa_s
);
5125 if (!wpa_s
->ap_iface
&&
5126 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout
,
5128 wpas_p2p_set_group_idle_timeout(wpa_s
);
5132 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
5134 struct wpa_supplicant
*iface
;
5136 if (!wpa_s
->global
->cross_connection
)
5139 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
5142 if (iface
->drv_flags
&
5143 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
5145 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
5148 wpa_s
->cross_connect_enabled
= 1;
5149 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
5150 sizeof(wpa_s
->cross_connect_uplink
));
5151 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
5152 "%s to %s whenever uplink is available",
5153 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
5155 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
5156 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
5157 iface
->cross_connect_disallowed
||
5158 iface
->wpa_state
!= WPA_COMPLETED
)
5161 wpa_s
->cross_connect_in_use
= 1;
5162 wpa_msg(wpa_s
->parent
, MSG_INFO
,
5163 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
5164 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
5170 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
5172 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
5173 !wpa_s
->p2p_in_provisioning
)
5174 return 0; /* not P2P client operation */
5176 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
5178 if (wpa_s
!= wpa_s
->parent
)
5179 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
5181 if (wpa_s
->global
->p2p
)
5182 p2p_group_formation_failed(wpa_s
->global
->p2p
);
5184 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5185 wpa_s
->parent
, NULL
);
5187 wpas_group_formation_completed(wpa_s
, 0);
5192 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
5194 struct p2p_channels chan
;
5196 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
5199 os_memset(&chan
, 0, sizeof(chan
));
5200 if (wpas_p2p_setup_channels(wpa_s
, &chan
)) {
5201 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
5206 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
);
5210 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant
*wpa_s
,
5211 struct wpa_scan_results
*scan_res
)
5213 wpa_printf(MSG_DEBUG
, "P2P: Ignore scan results");
5217 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
5219 struct wpa_global
*global
= wpa_s
->global
;
5223 if (global
->p2p
== NULL
)
5226 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
5228 if (wpa_s
->pending_interface_name
[0] &&
5229 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
5232 peer
= p2p_get_go_neg_peer(global
->p2p
);
5234 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
5235 MACSTR
, MAC2STR(peer
));
5236 p2p_unauthorize(global
->p2p
, peer
);
5240 if (wpa_s
->scan_res_handler
== wpas_p2p_scan_res_join
) {
5241 wpa_printf(MSG_DEBUG
, "P2P: Stop pending scan for join");
5242 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_ignore
;
5246 if (wpa_s
->pending_pd_before_join
) {
5247 wpa_printf(MSG_DEBUG
, "P2P: Stop pending PD before join");
5248 wpa_s
->pending_pd_before_join
= 0;
5252 wpas_p2p_stop_find(wpa_s
);
5254 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
5255 if (wpa_s
== global
->p2p_group_formation
&&
5256 (wpa_s
->p2p_in_provisioning
||
5257 wpa_s
->parent
->pending_interface_type
==
5258 WPA_IF_P2P_CLIENT
)) {
5259 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
5260 "formation found - cancelling",
5263 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5264 wpa_s
->parent
, NULL
);
5265 if (wpa_s
->p2p_in_provisioning
) {
5266 wpas_group_formation_completed(wpa_s
, 0);
5269 wpas_p2p_group_delete(wpa_s
,
5270 P2P_GROUP_REMOVAL_REQUESTED
);
5276 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
5284 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
5286 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
5289 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
5290 "being available anymore");
5291 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_UNAVAILABLE
);
5295 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
5296 int freq_24
, int freq_5
, int freq_overall
)
5298 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
5299 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
5301 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
5305 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
5308 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
5310 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
5313 if (hwaddr_aton(addr
, peer
))
5316 return p2p_unauthorize(p2p
, peer
);
5321 * wpas_p2p_disconnect - Disconnect from a P2P Group
5322 * @wpa_s: Pointer to wpa_supplicant data
5323 * Returns: 0 on success, -1 on failure
5325 * This can be used to disconnect from a group in which the local end is a P2P
5326 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5327 * virtual network interface was created for this group, that interface will be
5328 * removed. Otherwise, only the configured P2P group network will be removed
5329 * from the interface.
5331 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
5337 return wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_REQUESTED
) < 0 ?
5342 int wpas_p2p_in_progress(struct wpa_supplicant
*wpa_s
)
5344 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5347 return p2p_in_progress(wpa_s
->global
->p2p
);
5351 void wpas_p2p_network_removed(struct wpa_supplicant
*wpa_s
,
5352 struct wpa_ssid
*ssid
)
5354 if (wpa_s
->p2p_in_provisioning
&& ssid
->p2p_group
&&
5355 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5356 wpa_s
->parent
, NULL
) > 0) {
5358 * Remove the network by scheduling the group formation
5359 * timeout to happen immediately. The teardown code
5360 * needs to be scheduled to run asynch later so that we
5361 * don't delete data from under ourselves unexpectedly.
5362 * Calling wpas_p2p_group_formation_timeout directly
5363 * causes a series of crashes in WPS failure scenarios.
5365 wpa_printf(MSG_DEBUG
, "P2P: Canceled group formation due to "
5366 "P2P group network getting removed");
5367 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
5368 wpa_s
->parent
, NULL
);
5373 struct wpa_ssid
* wpas_p2p_get_persistent(struct wpa_supplicant
*wpa_s
,
5374 const u8
*addr
, const u8
*ssid
,
5380 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
5381 if (s
->disabled
!= 2)
5384 (ssid_len
!= s
->ssid_len
||
5385 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0))
5387 if (os_memcmp(s
->bssid
, addr
, ETH_ALEN
) == 0)
5388 return s
; /* peer is GO in the persistent group */
5389 if (s
->mode
!= WPAS_MODE_P2P_GO
|| s
->p2p_client_list
== NULL
)
5391 for (i
= 0; i
< s
->num_p2p_clients
; i
++) {
5392 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
,
5393 addr
, ETH_ALEN
) == 0)
5394 return s
; /* peer is P2P client in persistent
5403 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant
*wpa_s
,
5408 wpas_p2p_add_persistent_group_client(wpa_s
, addr
);
5412 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
5415 struct wpa_supplicant
*group
= wpa_s
;
5416 if (wpa_s
->global
->p2p_group_formation
)
5417 group
= wpa_s
->global
->p2p_group_formation
;
5418 wpa_s
= wpa_s
->parent
;
5419 offchannel_send_action_done(wpa_s
);
5421 wpas_p2p_group_delete(group
, P2P_GROUP_REMOVAL_SILENT
);
5422 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Fall back to GO Negotiation");
5423 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
, wpa_s
->p2p_pin
,
5424 wpa_s
->p2p_wps_method
, wpa_s
->p2p_persistent_group
, 0,
5425 0, 0, wpa_s
->p2p_go_intent
, wpa_s
->p2p_connect_freq
,
5426 wpa_s
->p2p_persistent_id
,
5427 wpa_s
->p2p_pd_before_go_neg
,
5428 wpa_s
->p2p_go_ht40
);
5432 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant
*wpa_s
)
5434 if (!wpa_s
->p2p_fallback_to_go_neg
||
5435 wpa_s
->p2p_in_provisioning
<= 5)
5438 if (wpas_p2p_peer_go(wpa_s
, wpa_s
->pending_join_dev_addr
) > 0)
5439 return 0; /* peer operating as a GO */
5441 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: GO not found for p2p_connect-auto - "
5442 "fallback to GO Negotiation");
5443 wpas_p2p_fallback_to_go_neg(wpa_s
, 1);
5449 unsigned int wpas_p2p_search_delay(struct wpa_supplicant
*wpa_s
)
5451 const char *rn
, *rn2
;
5452 struct wpa_supplicant
*ifs
;
5454 if (wpa_s
->wpa_state
> WPA_SCANNING
) {
5455 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search delay due to "
5456 "concurrent operation",
5457 P2P_CONCURRENT_SEARCH_DELAY
);
5458 return P2P_CONCURRENT_SEARCH_DELAY
;
5461 if (!wpa_s
->driver
->get_radio_name
)
5463 rn
= wpa_s
->driver
->get_radio_name(wpa_s
->drv_priv
);
5464 if (rn
== NULL
|| rn
[0] == '\0')
5467 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
5468 if (ifs
== wpa_s
|| !ifs
->driver
->get_radio_name
)
5471 rn2
= ifs
->driver
->get_radio_name(ifs
->drv_priv
);
5472 if (!rn2
|| os_strcmp(rn
, rn2
) != 0)
5474 if (ifs
->wpa_state
> WPA_SCANNING
) {
5475 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search "
5476 "delay due to concurrent operation on "
5478 P2P_CONCURRENT_SEARCH_DELAY
, ifs
->ifname
);
5479 return P2P_CONCURRENT_SEARCH_DELAY
;