3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "common/ieee802_11_common.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_i.h"
18 #include "ap/hostapd.h"
19 #include "ap/ap_config.h"
20 #include "ap/sta_info.h"
21 #include "ap/ap_drv_ops.h"
22 #include "ap/p2p_hostapd.h"
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "rsn_supp/wpa.h"
25 #include "wpa_supplicant_i.h"
28 #include "config_ssid.h"
33 #include "offchannel.h"
34 #include "wps_supplicant.h"
35 #include "p2p_supplicant.h"
36 #include "wifi_display.h"
40 * How many times to try to scan to find the GO before giving up on join
43 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
45 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
47 #ifndef P2P_MAX_CLIENT_IDLE
49 * How many seconds to try to reconnect to the GO when connection in P2P client
52 #define P2P_MAX_CLIENT_IDLE 10
53 #endif /* P2P_MAX_CLIENT_IDLE */
55 #ifndef P2P_MAX_INITIAL_CONN_WAIT
57 * How many seconds to wait for initial 4-way handshake to get completed after
58 * WPS provisioning step.
60 #define P2P_MAX_INITIAL_CONN_WAIT 10
61 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
63 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
65 * How many seconds to wait for initial 4-way handshake to get completed after
66 * WPS provisioning step on the GO. This controls the extra time the P2P
67 * operation is considered to be in progress (e.g., to delay other scans) after
68 * WPS provisioning has been completed on the GO during group formation.
70 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
71 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
73 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
75 * How many seconds to wait for initial 4-way handshake to get completed after
76 * re-invocation of a persistent group on the GO when the client is expected
77 * to connect automatically (no user interaction).
79 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
80 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
82 #ifndef P2P_CONCURRENT_SEARCH_DELAY
83 #define P2P_CONCURRENT_SEARCH_DELAY 500
84 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
86 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
88 enum p2p_group_removal_reason
{
89 P2P_GROUP_REMOVAL_UNKNOWN
,
90 P2P_GROUP_REMOVAL_SILENT
,
91 P2P_GROUP_REMOVAL_FORMATION_FAILED
,
92 P2P_GROUP_REMOVAL_REQUESTED
,
93 P2P_GROUP_REMOVAL_IDLE_TIMEOUT
,
94 P2P_GROUP_REMOVAL_UNAVAILABLE
,
95 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
,
96 P2P_GROUP_REMOVAL_PSK_FAILURE
,
97 P2P_GROUP_REMOVAL_FREQ_CONFLICT
101 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
102 static struct wpa_supplicant
*
103 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
105 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
);
106 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
);
107 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
);
108 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
109 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
111 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
);
112 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
);
113 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
);
114 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
);
115 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
117 static void wpas_p2p_group_freq_conflict(void *eloop_ctx
, void *timeout_ctx
);
118 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
120 static int wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
);
124 * Get the number of concurrent channels that the HW can operate, but that are
125 * currently not in use by any of the wpa_supplicant interfaces.
127 static int wpas_p2p_num_unused_channels(struct wpa_supplicant
*wpa_s
)
132 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
136 num
= get_shared_radio_freqs(wpa_s
, freqs
,
137 wpa_s
->num_multichan_concurrent
);
140 unused
= wpa_s
->num_multichan_concurrent
- num
;
141 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: num_unused_channels: %d", unused
);
147 * Get the frequencies that are currently in use by one or more of the virtual
148 * interfaces, and that are also valid for P2P operation.
150 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant
*wpa_s
,
151 int *p2p_freqs
, unsigned int len
)
154 unsigned int num
, i
, j
;
156 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
160 num
= get_shared_radio_freqs(wpa_s
, freqs
,
161 wpa_s
->num_multichan_concurrent
);
163 os_memset(p2p_freqs
, 0, sizeof(int) * len
);
165 for (i
= 0, j
= 0; i
< num
&& j
< len
; i
++) {
166 if (p2p_supported_freq(wpa_s
->global
->p2p
, freqs
[i
]))
167 p2p_freqs
[j
++] = freqs
[i
];
172 dump_freq_array(wpa_s
, "valid for P2P", p2p_freqs
, j
);
178 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant
*wpa_s
,
181 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
183 if (wpa_s
->parent
->conf
->p2p_ignore_shared_freq
&&
184 freq
> 0 && wpa_s
->num_multichan_concurrent
> 1 &&
185 wpas_p2p_num_unused_channels(wpa_s
) > 0) {
186 wpa_printf(MSG_DEBUG
, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
190 p2p_set_own_freq_preference(wpa_s
->global
->p2p
, freq
);
194 static void wpas_p2p_scan_res_handler(struct wpa_supplicant
*wpa_s
,
195 struct wpa_scan_results
*scan_res
)
199 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
202 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS)",
203 (int) scan_res
->num
);
205 for (i
= 0; i
< scan_res
->num
; i
++) {
206 struct wpa_scan_res
*bss
= scan_res
->res
[i
];
207 struct os_time time_tmp_age
, entry_ts
;
211 time_tmp_age
.sec
= bss
->age
/ 1000;
212 time_tmp_age
.usec
= (bss
->age
% 1000) * 1000;
213 os_time_sub(&scan_res
->fetch_time
, &time_tmp_age
, &entry_ts
);
215 ies
= (const u8
*) (bss
+ 1);
216 ies_len
= bss
->ie_len
;
217 if (bss
->beacon_ie_len
> 0 &&
218 !wpa_scan_get_vendor_ie(bss
, P2P_IE_VENDOR_TYPE
) &&
219 wpa_scan_get_vendor_ie_beacon(bss
, P2P_IE_VENDOR_TYPE
)) {
220 wpa_printf(MSG_DEBUG
, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
221 MACSTR
, MAC2STR(bss
->bssid
));
223 ies_len
= bss
->beacon_ie_len
;
227 if (p2p_scan_res_handler(wpa_s
->global
->p2p
, bss
->bssid
,
228 bss
->freq
, &entry_ts
, bss
->level
,
233 p2p_scan_res_handled(wpa_s
->global
->p2p
);
237 static int wpas_p2p_scan(void *ctx
, enum p2p_scan_type type
, int freq
,
238 unsigned int num_req_dev_types
,
239 const u8
*req_dev_types
, const u8
*dev_id
, u16 pw_id
)
241 struct wpa_supplicant
*wpa_s
= ctx
;
242 struct wpa_supplicant
*ifs
;
243 struct wpa_driver_scan_params params
;
245 struct wpabuf
*wps_ie
, *ies
;
246 int social_channels
[] = { 2412, 2437, 2462, 0, 0 };
249 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
252 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
253 if (ifs
->sta_scan_pending
&&
254 (wpas_scan_scheduled(ifs
) || ifs
->scanning
) &&
255 wpas_p2p_in_progress(wpa_s
) == 2) {
256 wpa_printf(MSG_DEBUG
, "Delaying P2P scan to allow "
257 "pending station mode scan to be "
258 "completed on interface %s", ifs
->ifname
);
259 wpa_s
->global
->p2p_cb_on_scan_complete
= 1;
260 wpa_supplicant_req_scan(ifs
, 0, 0);
265 os_memset(¶ms
, 0, sizeof(params
));
267 /* P2P Wildcard SSID */
268 params
.num_ssids
= 1;
269 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
270 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
272 wpa_s
->wps
->dev
.p2p
= 1;
273 wps_ie
= wps_build_probe_req_ie(pw_id
, &wpa_s
->wps
->dev
,
274 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
,
275 num_req_dev_types
, req_dev_types
);
279 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
280 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
285 wpabuf_put_buf(ies
, wps_ie
);
288 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, dev_id
);
290 params
.p2p_probe
= 1;
291 params
.extra_ies
= wpabuf_head(ies
);
292 params
.extra_ies_len
= wpabuf_len(ies
);
295 case P2P_SCAN_SOCIAL
:
296 params
.freqs
= social_channels
;
300 case P2P_SCAN_SOCIAL_PLUS_ONE
:
301 social_channels
[3] = freq
;
302 params
.freqs
= social_channels
;
306 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
311 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
313 ifs
->scan_res_handler
== wpas_p2p_scan_res_handler
) {
314 wpa_s
->global
->p2p_cb_on_scan_complete
= 1;
320 os_get_time(&wpa_s
->scan_trigger_time
);
321 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
328 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
330 switch (p2p_group_interface
) {
331 case P2P_GROUP_INTERFACE_PENDING
:
332 return WPA_IF_P2P_GROUP
;
333 case P2P_GROUP_INTERFACE_GO
:
334 return WPA_IF_P2P_GO
;
335 case P2P_GROUP_INTERFACE_CLIENT
:
336 return WPA_IF_P2P_CLIENT
;
339 return WPA_IF_P2P_GROUP
;
343 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
345 size_t ssid_len
, int *go
)
349 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
350 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
351 if (s
->disabled
!= 0 || !s
->p2p_group
||
352 s
->ssid_len
!= ssid_len
||
353 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
355 if (s
->mode
== WPAS_MODE_P2P_GO
&&
356 s
!= wpa_s
->current_ssid
)
359 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
368 static int wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
,
369 enum p2p_group_removal_reason removal_reason
)
371 struct wpa_ssid
*ssid
;
375 ssid
= wpa_s
->current_ssid
;
378 * The current SSID was not known, but there may still be a
379 * pending P2P group interface waiting for provisioning or a
380 * P2P group that is trying to reconnect.
382 ssid
= wpa_s
->conf
->ssid
;
384 if (ssid
->p2p_group
&& ssid
->disabled
!= 2)
389 wpa_s
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)
391 wpa_printf(MSG_ERROR
, "P2P: P2P group interface "
396 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
398 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
399 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
400 wpa_s
->reassociate
= 0;
401 wpa_s
->disconnected
= 1;
402 wpa_supplicant_deauthenticate(wpa_s
,
403 WLAN_REASON_DEAUTH_LEAVING
);
407 if (wpa_s
->cross_connect_in_use
) {
408 wpa_s
->cross_connect_in_use
= 0;
409 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
410 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
411 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
413 switch (removal_reason
) {
414 case P2P_GROUP_REMOVAL_REQUESTED
:
415 reason
= " reason=REQUESTED";
417 case P2P_GROUP_REMOVAL_FORMATION_FAILED
:
418 reason
= " reason=FORMATION_FAILED";
420 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
421 reason
= " reason=IDLE";
423 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
424 reason
= " reason=UNAVAILABLE";
426 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION
:
427 reason
= " reason=GO_ENDING_SESSION";
429 case P2P_GROUP_REMOVAL_PSK_FAILURE
:
430 reason
= " reason=PSK_FAILURE";
432 case P2P_GROUP_REMOVAL_FREQ_CONFLICT
:
433 reason
= " reason=FREQ_CONFLICT";
439 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
) {
440 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
441 P2P_EVENT_GROUP_REMOVED
"%s %s%s",
442 wpa_s
->ifname
, gtype
, reason
);
445 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict
, wpa_s
, NULL
) > 0)
446 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group freq_conflict timeout");
447 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
448 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
449 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
450 wpa_s
->parent
, NULL
) > 0) {
451 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group formation "
453 wpa_s
->p2p_in_provisioning
= 0;
457 * Make sure wait for the first client does not remain active after the
458 * group has been removed.
460 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
462 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
&& ssid
)
463 wpas_notify_p2p_group_removed(wpa_s
, ssid
, gtype
);
465 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
466 struct wpa_global
*global
;
468 enum wpa_driver_if_type type
;
469 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
471 global
= wpa_s
->global
;
472 ifname
= os_strdup(wpa_s
->ifname
);
473 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
474 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
, 0);
475 wpa_s
= global
->ifaces
;
477 wpa_drv_if_remove(wpa_s
, type
, ifname
);
482 if (!wpa_s
->p2p_go_group_formation_completed
) {
483 wpa_s
->global
->p2p_group_formation
= NULL
;
484 wpa_s
->p2p_in_provisioning
= 0;
487 wpa_s
->show_group_started
= 0;
488 os_free(wpa_s
->go_params
);
489 wpa_s
->go_params
= NULL
;
491 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
492 if (ssid
&& (ssid
->p2p_group
||
493 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
494 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
496 if (ssid
== wpa_s
->current_ssid
) {
497 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
498 eapol_sm_notify_config(wpa_s
->eapol
, NULL
, NULL
);
499 wpa_s
->current_ssid
= NULL
;
502 * Networks objects created during any P2P activities are not
503 * exposed out as they might/will confuse certain non-P2P aware
504 * applications since these network objects won't behave like
507 * Likewise, we don't send out network removed signals for such
510 wpa_config_remove_network(wpa_s
->conf
, id
);
511 wpa_supplicant_clear_status(wpa_s
);
512 wpa_supplicant_cancel_sched_scan(wpa_s
);
513 wpa_s
->sta_scan_pending
= 0;
515 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
519 wpa_supplicant_ap_deinit(wpa_s
);
521 wpa_drv_deinit_p2p_cli(wpa_s
);
527 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
529 const u8
*ssid
, size_t ssid_len
)
537 if (wpa_s
->go_params
)
538 bssid
= wpa_s
->go_params
->peer_interface_addr
;
540 bssid
= wpa_s
->bssid
;
542 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
544 u8 iface_addr
[ETH_ALEN
];
545 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
547 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
550 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
551 "group is persistent - BSS " MACSTR
" not found",
556 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
558 p2p
= wpa_bss_get_vendor_ie_multi_beacon(bss
,
561 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
562 "group is persistent - BSS " MACSTR
563 " did not include P2P IE", MAC2STR(bssid
));
564 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
565 (u8
*) (bss
+ 1), bss
->ie_len
);
566 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
567 ((u8
*) bss
+ 1) + bss
->ie_len
,
572 group_capab
= p2p_get_group_capab(p2p
);
573 addr
= p2p_get_go_dev_addr(p2p
);
574 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
575 "group_capab=0x%x", group_capab
);
577 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
578 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
581 os_memset(go_dev_addr
, 0, ETH_ALEN
);
584 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
585 "go_dev_addr=" MACSTR
,
586 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
588 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
592 static int wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
593 struct wpa_ssid
*ssid
,
594 const u8
*go_dev_addr
)
599 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
600 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
601 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
602 if (s
->disabled
== 2 &&
603 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
604 s
->ssid_len
== ssid
->ssid_len
&&
605 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
610 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
612 if (ssid
->passphrase
&& !s
->passphrase
)
614 else if (ssid
->passphrase
&& s
->passphrase
&&
615 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
618 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
621 s
= wpa_config_add_network(wpa_s
->conf
);
626 * Instead of network_added we emit persistent_group_added
627 * notification. Also to keep the defense checks in
628 * persistent_group obj registration method, we set the
629 * relevant flags in s to designate it as a persistent group.
632 s
->p2p_persistent_group
= 1;
633 wpas_notify_persistent_group_added(wpa_s
, s
);
634 wpa_config_set_network_defaults(s
);
638 s
->p2p_persistent_group
= 1;
641 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
642 s
->mode
= ssid
->mode
;
643 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
644 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
645 s
->proto
= WPA_PROTO_RSN
;
646 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
648 if (ssid
->passphrase
) {
649 os_free(s
->passphrase
);
650 s
->passphrase
= os_strdup(ssid
->passphrase
);
654 os_memcpy(s
->psk
, ssid
->psk
, 32);
656 if (s
->passphrase
&& !s
->psk_set
)
657 wpa_config_update_psk(s
);
658 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
660 s
->ssid
= os_malloc(ssid
->ssid_len
);
663 s
->ssid_len
= ssid
->ssid_len
;
664 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
666 if (ssid
->mode
== WPAS_MODE_P2P_GO
&& wpa_s
->global
->add_psk
) {
667 dl_list_add(&s
->psk_list
, &wpa_s
->global
->add_psk
->list
);
668 wpa_s
->global
->add_psk
= NULL
;
672 #ifndef CONFIG_NO_CONFIG_WRITE
673 if (changed
&& wpa_s
->conf
->update_config
&&
674 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
675 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
677 #endif /* CONFIG_NO_CONFIG_WRITE */
683 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant
*wpa_s
,
686 struct wpa_ssid
*ssid
, *s
;
691 ssid
= wpa_s
->current_ssid
;
692 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
693 !ssid
->p2p_persistent_group
)
696 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
697 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
700 if (s
->ssid_len
== ssid
->ssid_len
&&
701 os_memcmp(s
->ssid
, ssid
->ssid
, s
->ssid_len
) == 0)
708 for (i
= 0; s
->p2p_client_list
&& i
< s
->num_p2p_clients
; i
++) {
709 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
, addr
,
713 if (i
== s
->num_p2p_clients
- 1)
714 return; /* already the most recent entry */
716 /* move the entry to mark it most recent */
717 os_memmove(s
->p2p_client_list
+ i
* ETH_ALEN
,
718 s
->p2p_client_list
+ (i
+ 1) * ETH_ALEN
,
719 (s
->num_p2p_clients
- i
- 1) * ETH_ALEN
);
720 os_memcpy(s
->p2p_client_list
+
721 (s
->num_p2p_clients
- 1) * ETH_ALEN
, addr
, ETH_ALEN
);
726 if (!found
&& s
->num_p2p_clients
< P2P_MAX_STORED_CLIENTS
) {
727 n
= os_realloc_array(s
->p2p_client_list
,
728 s
->num_p2p_clients
+ 1, ETH_ALEN
);
731 os_memcpy(n
+ s
->num_p2p_clients
* ETH_ALEN
, addr
, ETH_ALEN
);
732 s
->p2p_client_list
= n
;
733 s
->num_p2p_clients
++;
735 /* Not enough room for an additional entry - drop the oldest
737 os_memmove(s
->p2p_client_list
,
738 s
->p2p_client_list
+ ETH_ALEN
,
739 (s
->num_p2p_clients
- 1) * ETH_ALEN
);
740 os_memcpy(s
->p2p_client_list
+
741 (s
->num_p2p_clients
- 1) * ETH_ALEN
,
745 #ifndef CONFIG_NO_CONFIG_WRITE
746 if (wpa_s
->parent
->conf
->update_config
&&
747 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
748 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
749 #endif /* CONFIG_NO_CONFIG_WRITE */
753 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
756 struct wpa_ssid
*ssid
;
757 const char *ssid_txt
;
760 u8 go_dev_addr
[ETH_ALEN
];
764 * This callback is likely called for the main interface. Update wpa_s
765 * to use the group interface if a new interface was created for the
768 if (wpa_s
->global
->p2p_group_formation
)
769 wpa_s
= wpa_s
->global
->p2p_group_formation
;
770 if (wpa_s
->p2p_go_group_formation_completed
) {
771 wpa_s
->global
->p2p_group_formation
= NULL
;
772 wpa_s
->p2p_in_provisioning
= 0;
776 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
777 P2P_EVENT_GROUP_FORMATION_FAILURE
);
778 wpas_p2p_group_delete(wpa_s
,
779 P2P_GROUP_REMOVAL_FORMATION_FAILED
);
783 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
784 P2P_EVENT_GROUP_FORMATION_SUCCESS
);
786 ssid
= wpa_s
->current_ssid
;
787 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
788 ssid
->mode
= WPAS_MODE_P2P_GO
;
789 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
790 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
795 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
796 client
= ssid
->mode
== WPAS_MODE_INFRA
;
797 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
798 persistent
= ssid
->p2p_persistent_group
;
799 os_memcpy(go_dev_addr
, wpa_s
->global
->p2p_dev_addr
,
802 persistent
= wpas_p2p_persistent_group(wpa_s
,
808 client
= wpa_s
->p2p_group_interface
==
809 P2P_GROUP_INTERFACE_CLIENT
;
810 os_memset(go_dev_addr
, 0, ETH_ALEN
);
813 wpa_s
->show_group_started
= 0;
816 * Indicate event only after successfully completed 4-way
817 * handshake, i.e., when the interface is ready for data
820 wpa_s
->show_group_started
= 1;
821 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
823 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
824 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
825 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
827 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
828 MAC2STR(go_dev_addr
),
829 persistent
? " [PERSISTENT]" : "");
830 wpas_p2p_cross_connect_setup(wpa_s
);
831 wpas_p2p_set_group_idle_timeout(wpa_s
);
833 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
834 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
835 "go_dev_addr=" MACSTR
"%s",
836 wpa_s
->ifname
, ssid_txt
,
837 ssid
? ssid
->frequency
: 0,
838 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
839 MAC2STR(go_dev_addr
),
840 persistent
? " [PERSISTENT]" : "");
841 wpas_p2p_cross_connect_setup(wpa_s
);
842 wpas_p2p_set_group_idle_timeout(wpa_s
);
846 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
849 os_free(wpa_s
->global
->add_psk
);
850 wpa_s
->global
->add_psk
= NULL
;
852 if (network_id
< 0 && ssid
)
853 network_id
= ssid
->id
;
855 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
856 os_get_time(&wpa_s
->global
->p2p_go_wait_client
);
861 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant
*wpa_s
,
863 const u8
*dst
, const u8
*src
,
865 const u8
*data
, size_t data_len
,
866 enum offchannel_send_action_result
869 enum p2p_send_action_result res
= P2P_SEND_ACTION_SUCCESS
;
871 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
873 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
877 case OFFCHANNEL_SEND_ACTION_SUCCESS
:
878 res
= P2P_SEND_ACTION_SUCCESS
;
880 case OFFCHANNEL_SEND_ACTION_NO_ACK
:
881 res
= P2P_SEND_ACTION_NO_ACK
;
883 case OFFCHANNEL_SEND_ACTION_FAILED
:
884 res
= P2P_SEND_ACTION_FAILED
;
888 p2p_send_action_cb(wpa_s
->global
->p2p
, freq
, dst
, src
, bssid
, res
);
890 if (result
!= OFFCHANNEL_SEND_ACTION_SUCCESS
&&
891 wpa_s
->pending_pd_before_join
&&
892 (os_memcmp(dst
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
893 os_memcmp(dst
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0) &&
894 wpa_s
->p2p_fallback_to_go_neg
) {
895 wpa_s
->pending_pd_before_join
= 0;
896 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No ACK for PD Req "
897 "during p2p_connect-auto");
898 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
904 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
905 const u8
*src
, const u8
*bssid
, const u8
*buf
,
906 size_t len
, unsigned int wait_time
)
908 struct wpa_supplicant
*wpa_s
= ctx
;
909 return offchannel_send_action(wpa_s
, freq
, dst
, src
, bssid
, buf
, len
,
911 wpas_p2p_send_action_tx_status
, 1);
915 static void wpas_send_action_done(void *ctx
)
917 struct wpa_supplicant
*wpa_s
= ctx
;
918 offchannel_send_action_done(wpa_s
);
922 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
923 struct p2p_go_neg_results
*params
)
925 if (wpa_s
->go_params
== NULL
) {
926 wpa_s
->go_params
= os_malloc(sizeof(*params
));
927 if (wpa_s
->go_params
== NULL
)
930 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
935 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
936 struct p2p_go_neg_results
*res
)
938 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
939 MAC2STR(res
->peer_interface_addr
));
940 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
941 res
->ssid
, res
->ssid_len
);
942 wpa_supplicant_ap_deinit(wpa_s
);
943 wpas_copy_go_neg_results(wpa_s
, res
);
944 if (res
->wps_method
== WPS_PBC
)
945 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
947 u16 dev_pw_id
= DEV_PW_DEFAULT
;
948 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
949 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
950 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
951 wpa_s
->p2p_pin
, 1, dev_pw_id
);
956 static void wpas_p2p_add_psk_list(struct wpa_supplicant
*wpa_s
,
957 struct wpa_ssid
*ssid
)
959 struct wpa_ssid
*persistent
;
960 struct psk_list_entry
*psk
;
961 struct hostapd_data
*hapd
;
963 if (!wpa_s
->ap_iface
)
966 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, NULL
, ssid
->ssid
,
968 if (persistent
== NULL
)
971 hapd
= wpa_s
->ap_iface
->bss
[0];
973 dl_list_for_each(psk
, &persistent
->psk_list
, struct psk_list_entry
,
975 struct hostapd_wpa_psk
*hpsk
;
977 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add persistent group PSK entry for "
979 MAC2STR(psk
->addr
), psk
->p2p
);
980 hpsk
= os_zalloc(sizeof(*hpsk
));
983 os_memcpy(hpsk
->psk
, psk
->psk
, PMK_LEN
);
985 os_memcpy(hpsk
->p2p_dev_addr
, psk
->addr
, ETH_ALEN
);
987 os_memcpy(hpsk
->addr
, psk
->addr
, ETH_ALEN
);
988 hpsk
->next
= hapd
->conf
->ssid
.wpa_psk
;
989 hapd
->conf
->ssid
.wpa_psk
= hpsk
;
994 static void p2p_go_configured(void *ctx
, void *data
)
996 struct wpa_supplicant
*wpa_s
= ctx
;
997 struct p2p_go_neg_results
*params
= data
;
998 struct wpa_ssid
*ssid
;
1001 ssid
= wpa_s
->current_ssid
;
1002 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
1003 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
1004 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
1005 wpa_s
->global
->p2p_group_formation
= NULL
;
1006 if (os_strlen(params
->passphrase
) > 0) {
1007 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
1008 P2P_EVENT_GROUP_STARTED
1009 "%s GO ssid=\"%s\" freq=%d "
1010 "passphrase=\"%s\" go_dev_addr=" MACSTR
1011 "%s", wpa_s
->ifname
,
1012 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
1013 ssid
->frequency
, params
->passphrase
,
1014 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
1015 params
->persistent_group
?
1016 " [PERSISTENT]" : "");
1019 wpa_snprintf_hex(psk
, sizeof(psk
), params
->psk
,
1020 sizeof(params
->psk
));
1021 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
1022 P2P_EVENT_GROUP_STARTED
1023 "%s GO ssid=\"%s\" freq=%d psk=%s "
1024 "go_dev_addr=" MACSTR
"%s",
1026 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
1027 ssid
->frequency
, psk
,
1028 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
1029 params
->persistent_group
?
1030 " [PERSISTENT]" : "");
1033 os_get_time(&wpa_s
->global
->p2p_go_wait_client
);
1034 if (params
->persistent_group
) {
1035 network_id
= wpas_p2p_store_persistent_group(
1036 wpa_s
->parent
, ssid
,
1037 wpa_s
->global
->p2p_dev_addr
);
1038 wpas_p2p_add_psk_list(wpa_s
, ssid
);
1041 network_id
= ssid
->id
;
1042 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
1043 wpas_p2p_cross_connect_setup(wpa_s
);
1044 wpas_p2p_set_group_idle_timeout(wpa_s
);
1046 if (wpa_s
->p2p_first_connection_timeout
) {
1047 wpa_dbg(wpa_s
, MSG_DEBUG
,
1048 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1049 wpa_s
->p2p_first_connection_timeout
);
1050 wpa_s
->p2p_go_group_formation_completed
= 0;
1051 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1052 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
1053 wpa_s
->parent
, NULL
);
1054 eloop_register_timeout(
1055 wpa_s
->p2p_first_connection_timeout
, 0,
1056 wpas_p2p_group_formation_timeout
,
1057 wpa_s
->parent
, NULL
);
1063 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
1064 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
1065 params
->peer_interface_addr
)) {
1066 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
1070 if (params
->wps_method
== WPS_PBC
)
1071 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
1072 params
->peer_device_addr
);
1073 else if (wpa_s
->p2p_pin
[0])
1074 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
1075 wpa_s
->p2p_pin
, NULL
, 0, 0);
1076 os_free(wpa_s
->go_params
);
1077 wpa_s
->go_params
= NULL
;
1081 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
1082 struct p2p_go_neg_results
*params
,
1083 int group_formation
)
1085 struct wpa_ssid
*ssid
;
1087 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Starting GO");
1088 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0) {
1089 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not copy GO Negotiation "
1094 ssid
= wpa_config_add_network(wpa_s
->conf
);
1096 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not add network for GO");
1100 wpa_s
->show_group_started
= 0;
1102 wpa_config_set_network_defaults(ssid
);
1103 ssid
->temporary
= 1;
1104 ssid
->p2p_group
= 1;
1105 ssid
->p2p_persistent_group
= params
->persistent_group
;
1106 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
1108 ssid
->frequency
= params
->freq
;
1109 ssid
->ht40
= params
->ht40
;
1110 ssid
->vht
= params
->vht
;
1111 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
1113 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
1114 ssid
->ssid_len
= params
->ssid_len
;
1116 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
1117 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
1118 ssid
->proto
= WPA_PROTO_RSN
;
1119 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
1120 if (os_strlen(params
->passphrase
) > 0) {
1121 ssid
->passphrase
= os_strdup(params
->passphrase
);
1122 if (ssid
->passphrase
== NULL
) {
1123 wpa_msg_global(wpa_s
, MSG_ERROR
,
1124 "P2P: Failed to copy passphrase for GO");
1125 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1129 ssid
->passphrase
= NULL
;
1130 ssid
->psk_set
= params
->psk_set
;
1132 os_memcpy(ssid
->psk
, params
->psk
, sizeof(ssid
->psk
));
1133 else if (ssid
->passphrase
)
1134 wpa_config_update_psk(ssid
);
1135 ssid
->ap_max_inactivity
= wpa_s
->parent
->conf
->p2p_go_max_inactivity
;
1137 wpa_s
->ap_configured_cb
= p2p_go_configured
;
1138 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
1139 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
1140 wpa_s
->connect_without_scan
= ssid
;
1141 wpa_s
->reassociate
= 1;
1142 wpa_s
->disconnected
= 0;
1143 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Request scan (that will be skipped) to "
1145 wpa_supplicant_req_scan(wpa_s
, 0, 0);
1149 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
1150 const struct wpa_supplicant
*src
)
1152 struct wpa_config
*d
;
1153 const struct wpa_config
*s
;
1158 #define C(n) if (s->n) d->n = os_strdup(s->n)
1167 os_memcpy(d
->device_type
, s
->device_type
, WPS_DEV_TYPE_LEN
);
1168 os_memcpy(d
->sec_device_type
, s
->sec_device_type
,
1169 sizeof(d
->sec_device_type
));
1170 d
->num_sec_device_types
= s
->num_sec_device_types
;
1172 d
->p2p_group_idle
= s
->p2p_group_idle
;
1173 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
1174 d
->persistent_reconnect
= s
->persistent_reconnect
;
1175 d
->max_num_sta
= s
->max_num_sta
;
1176 d
->pbc_in_m1
= s
->pbc_in_m1
;
1177 d
->ignore_old_scan_res
= s
->ignore_old_scan_res
;
1178 d
->beacon_int
= s
->beacon_int
;
1179 d
->disassoc_low_ack
= s
->disassoc_low_ack
;
1180 d
->disable_scan_offload
= s
->disable_scan_offload
;
1184 static void wpas_p2p_get_group_ifname(struct wpa_supplicant
*wpa_s
,
1185 char *ifname
, size_t len
)
1187 char *ifname_ptr
= wpa_s
->ifname
;
1189 if (os_strncmp(wpa_s
->ifname
, P2P_MGMT_DEVICE_PREFIX
,
1190 os_strlen(P2P_MGMT_DEVICE_PREFIX
)) == 0) {
1191 ifname_ptr
= os_strrchr(wpa_s
->ifname
, '-') + 1;
1194 os_snprintf(ifname
, len
, "p2p-%s-%d", ifname_ptr
, wpa_s
->p2p_group_idx
);
1195 if (os_strlen(ifname
) >= IFNAMSIZ
&&
1196 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
1197 /* Try to avoid going over the IFNAMSIZ length limit */
1198 os_snprintf(ifname
, len
, "p2p-%d", wpa_s
->p2p_group_idx
);
1203 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
1204 enum wpa_driver_if_type type
)
1206 char ifname
[120], force_ifname
[120];
1208 if (wpa_s
->pending_interface_name
[0]) {
1209 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
1210 "- skip creation of a new one");
1211 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
1212 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
1213 "unknown?! ifname='%s'",
1214 wpa_s
->pending_interface_name
);
1220 wpas_p2p_get_group_ifname(wpa_s
, ifname
, sizeof(ifname
));
1221 force_ifname
[0] = '\0';
1223 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
1225 wpa_s
->p2p_group_idx
++;
1227 wpa_s
->pending_interface_type
= type
;
1228 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
1229 wpa_s
->pending_interface_addr
, NULL
) < 0) {
1230 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
1235 if (force_ifname
[0]) {
1236 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
1238 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
1239 sizeof(wpa_s
->pending_interface_name
));
1241 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
1242 sizeof(wpa_s
->pending_interface_name
));
1243 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
1244 MACSTR
, wpa_s
->pending_interface_name
,
1245 MAC2STR(wpa_s
->pending_interface_addr
));
1251 static void wpas_p2p_remove_pending_group_interface(
1252 struct wpa_supplicant
*wpa_s
)
1254 if (!wpa_s
->pending_interface_name
[0] ||
1255 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
1256 return; /* No pending virtual interface */
1258 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
1259 wpa_s
->pending_interface_name
);
1260 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
1261 wpa_s
->pending_interface_name
);
1262 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1263 wpa_s
->pending_interface_name
[0] = '\0';
1267 static struct wpa_supplicant
*
1268 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1270 struct wpa_interface iface
;
1271 struct wpa_supplicant
*group_wpa_s
;
1273 if (!wpa_s
->pending_interface_name
[0]) {
1274 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1275 if (!wpas_p2p_create_iface(wpa_s
))
1278 * Something has forced us to remove the pending interface; try
1279 * to create a new one and hope for the best that we will get
1280 * the same local address.
1282 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1283 WPA_IF_P2P_CLIENT
) < 0)
1287 os_memset(&iface
, 0, sizeof(iface
));
1288 iface
.ifname
= wpa_s
->pending_interface_name
;
1289 iface
.driver
= wpa_s
->driver
->name
;
1290 if (wpa_s
->conf
->ctrl_interface
== NULL
&&
1291 wpa_s
->parent
!= wpa_s
&&
1293 (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE
))
1294 iface
.ctrl_interface
= wpa_s
->parent
->conf
->ctrl_interface
;
1296 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1297 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1298 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1299 if (group_wpa_s
== NULL
) {
1300 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1301 "wpa_supplicant interface");
1304 wpa_s
->pending_interface_name
[0] = '\0';
1305 group_wpa_s
->parent
= wpa_s
;
1306 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1307 P2P_GROUP_INTERFACE_CLIENT
;
1308 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1310 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
1316 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
1319 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1320 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
1321 wpas_p2p_group_formation_failed(wpa_s
);
1325 void wpas_p2p_group_formation_failed(struct wpa_supplicant
*wpa_s
)
1327 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
1328 wpa_s
->parent
, NULL
);
1329 if (wpa_s
->global
->p2p
)
1330 p2p_group_formation_failed(wpa_s
->global
->p2p
);
1331 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1332 wpa_drv_p2p_group_formation_failed(wpa_s
);
1333 wpas_group_formation_completed(wpa_s
, 0);
1337 void wpas_p2p_ap_setup_failed(struct wpa_supplicant
*wpa_s
)
1339 if (wpa_s
->global
->p2p_group_formation
!= wpa_s
)
1341 /* Speed up group formation timeout since this cannot succeed */
1342 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
1343 wpa_s
->parent
, NULL
);
1344 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
1345 wpa_s
->parent
, NULL
);
1349 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
1351 struct wpa_supplicant
*wpa_s
= ctx
;
1353 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1354 wpa_drv_cancel_remain_on_channel(wpa_s
);
1355 wpa_s
->off_channel_freq
= 0;
1356 wpa_s
->roc_waiting_drv_freq
= 0;
1360 wpa_msg_global(wpa_s
, MSG_INFO
,
1361 P2P_EVENT_GO_NEG_FAILURE
"status=%d",
1363 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
1364 wpas_p2p_remove_pending_group_interface(wpa_s
);
1368 if (wpa_s
->p2p_go_ht40
)
1370 if (wpa_s
->p2p_go_vht
)
1373 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
"role=%s "
1374 "freq=%d ht40=%d peer_dev=" MACSTR
" peer_iface=" MACSTR
1376 res
->role_go
? "GO" : "client", res
->freq
, res
->ht40
,
1377 MAC2STR(res
->peer_device_addr
),
1378 MAC2STR(res
->peer_interface_addr
),
1379 p2p_wps_method_text(res
->wps_method
));
1380 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
1382 if (res
->role_go
&& wpa_s
->p2p_persistent_id
>= 0) {
1383 struct wpa_ssid
*ssid
;
1384 ssid
= wpa_config_get_network(wpa_s
->conf
,
1385 wpa_s
->p2p_persistent_id
);
1386 if (ssid
&& ssid
->disabled
== 2 &&
1387 ssid
->mode
== WPAS_MODE_P2P_GO
&& ssid
->passphrase
) {
1388 size_t len
= os_strlen(ssid
->passphrase
);
1389 wpa_printf(MSG_DEBUG
, "P2P: Override passphrase based "
1390 "on requested persistent group");
1391 os_memcpy(res
->passphrase
, ssid
->passphrase
, len
);
1392 res
->passphrase
[len
] = '\0';
1396 if (wpa_s
->create_p2p_iface
) {
1397 struct wpa_supplicant
*group_wpa_s
=
1398 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
1399 if (group_wpa_s
== NULL
) {
1400 wpas_p2p_remove_pending_group_interface(wpa_s
);
1403 if (group_wpa_s
!= wpa_s
) {
1404 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
1405 sizeof(group_wpa_s
->p2p_pin
));
1406 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
1408 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1409 wpa_s
->pending_interface_name
[0] = '\0';
1410 group_wpa_s
->p2p_in_provisioning
= 1;
1413 wpas_start_wps_go(group_wpa_s
, res
, 1);
1415 wpas_start_wps_enrollee(group_wpa_s
, res
);
1417 wpa_s
->p2p_in_provisioning
= 1;
1418 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1421 wpas_start_wps_go(wpa_s
, res
, 1);
1423 wpas_start_wps_enrollee(ctx
, res
);
1426 wpa_s
->p2p_long_listen
= 0;
1427 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1429 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1430 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1431 (res
->peer_config_timeout
% 100) * 10000,
1432 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1436 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1438 struct wpa_supplicant
*wpa_s
= ctx
;
1439 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1440 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1442 wpas_notify_p2p_go_neg_req(wpa_s
, src
, dev_passwd_id
);
1446 void wpas_dev_found(void *ctx
, const u8
*addr
,
1447 const struct p2p_peer_info
*info
,
1450 #ifndef CONFIG_NO_STDOUT_DEBUG
1451 struct wpa_supplicant
*wpa_s
= ctx
;
1452 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1453 char *wfd_dev_info_hex
= NULL
;
1455 #ifdef CONFIG_WIFI_DISPLAY
1456 wfd_dev_info_hex
= wifi_display_subelem_hex(info
->wfd_subelems
,
1457 WFD_SUBELEM_DEVICE_INFO
);
1458 #endif /* CONFIG_WIFI_DISPLAY */
1460 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1461 " p2p_dev_addr=" MACSTR
1462 " pri_dev_type=%s name='%s' config_methods=0x%x "
1463 "dev_capab=0x%x group_capab=0x%x%s%s",
1464 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
1465 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
1467 info
->device_name
, info
->config_methods
,
1468 info
->dev_capab
, info
->group_capab
,
1469 wfd_dev_info_hex
? " wfd_dev_info=0x" : "",
1470 wfd_dev_info_hex
? wfd_dev_info_hex
: "");
1471 #endif /* CONFIG_NO_STDOUT_DEBUG */
1473 os_free(wfd_dev_info_hex
);
1475 wpas_notify_p2p_device_found(ctx
, info
->p2p_device_addr
, new_device
);
1479 static void wpas_dev_lost(void *ctx
, const u8
*dev_addr
)
1481 struct wpa_supplicant
*wpa_s
= ctx
;
1483 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_LOST
1484 "p2p_dev_addr=" MACSTR
, MAC2STR(dev_addr
));
1486 wpas_notify_p2p_device_lost(wpa_s
, dev_addr
);
1490 static void wpas_find_stopped(void *ctx
)
1492 struct wpa_supplicant
*wpa_s
= ctx
;
1493 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_FIND_STOPPED
);
1497 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1498 unsigned int duration
,
1499 const struct wpabuf
*probe_resp_ie
)
1501 struct wpa_supplicant
*wpa_s
= ctx
;
1503 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1505 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1506 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1507 "report received Probe Request frames");
1511 wpa_s
->pending_listen_freq
= freq
;
1512 wpa_s
->pending_listen_duration
= duration
;
1514 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1515 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1516 "to remain on channel (%u MHz) for Listen "
1518 wpa_s
->pending_listen_freq
= 0;
1521 wpa_s
->off_channel_freq
= 0;
1522 wpa_s
->roc_waiting_drv_freq
= freq
;
1528 static void wpas_stop_listen(void *ctx
)
1530 struct wpa_supplicant
*wpa_s
= ctx
;
1531 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1532 wpa_drv_cancel_remain_on_channel(wpa_s
);
1533 wpa_s
->off_channel_freq
= 0;
1534 wpa_s
->roc_waiting_drv_freq
= 0;
1536 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
1537 wpa_drv_probe_req_report(wpa_s
, 0);
1541 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1543 struct wpa_supplicant
*wpa_s
= ctx
;
1544 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
), 1);
1549 * DNS Header section is used only to calculate compression pointers, so the
1550 * contents of this data does not matter, but the length needs to be reserved
1551 * in the virtual packet.
1553 #define DNS_HEADER_LEN 12
1556 * 27-octet in-memory packet from P2P specification containing two implied
1557 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1559 #define P2P_SD_IN_MEMORY_LEN 27
1561 static int p2p_sd_dns_uncompress_label(char **upos
, char *uend
, u8
*start
,
1562 u8
**spos
, const u8
*end
)
1564 while (*spos
< end
) {
1565 u8 val
= ((*spos
)[0] & 0xc0) >> 6;
1568 if (val
== 1 || val
== 2) {
1569 /* These are reserved values in RFC 1035 */
1570 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
1571 "sequence starting with 0x%x", val
);
1580 if (*spos
+ 2 > end
) {
1581 wpa_printf(MSG_DEBUG
, "P2P: No room for full "
1582 "DNS offset field");
1586 offset
= (((*spos
)[0] & 0x3f) << 8) | (*spos
)[1];
1587 if (offset
>= *spos
- start
) {
1588 wpa_printf(MSG_DEBUG
, "P2P: Invalid DNS "
1589 "pointer offset %u", offset
);
1594 spos_tmp
= start
+ offset
;
1595 return p2p_sd_dns_uncompress_label(upos
, uend
, start
,
1601 len
= (*spos
)[0] & 0x3f;
1606 if (*spos
+ len
> end
) {
1607 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
1608 "sequence - no room for label with length "
1613 if (*upos
+ len
+ 2 > uend
)
1616 os_memcpy(*upos
, *spos
, len
);
1628 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1629 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1630 * not large enough */
1631 static int p2p_sd_dns_uncompress(char *buf
, size_t buf_len
, const u8
*msg
,
1632 size_t msg_len
, size_t offset
)
1634 /* 27-octet in-memory packet from P2P specification */
1635 const char *prefix
= "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1636 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1637 u8
*tmp
, *end
, *spos
;
1643 if (offset
> msg_len
)
1646 tmp
= os_malloc(DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
+ msg_len
);
1649 spos
= tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
;
1650 end
= spos
+ msg_len
;
1653 os_memset(tmp
, 0, DNS_HEADER_LEN
);
1654 os_memcpy(tmp
+ DNS_HEADER_LEN
, prefix
, P2P_SD_IN_MEMORY_LEN
);
1655 os_memcpy(tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
, msg
, msg_len
);
1658 uend
= buf
+ buf_len
;
1660 ret
= p2p_sd_dns_uncompress_label(&upos
, uend
, tmp
, &spos
, end
);
1669 } else if (upos
[-1] == '.')
1677 static struct p2p_srv_bonjour
*
1678 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1679 const struct wpabuf
*query
)
1681 struct p2p_srv_bonjour
*bsrv
;
1684 len
= wpabuf_len(query
);
1685 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1686 struct p2p_srv_bonjour
, list
) {
1687 if (len
== wpabuf_len(bsrv
->query
) &&
1688 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1696 static struct p2p_srv_upnp
*
1697 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1698 const char *service
)
1700 struct p2p_srv_upnp
*usrv
;
1702 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1703 struct p2p_srv_upnp
, list
) {
1704 if (version
== usrv
->version
&&
1705 os_strcmp(service
, usrv
->service
) == 0)
1712 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1717 if (wpabuf_tailroom(resp
) < 5)
1720 /* Length (to be filled) */
1721 len_pos
= wpabuf_put(resp
, 2);
1722 wpabuf_put_u8(resp
, srv_proto
);
1723 wpabuf_put_u8(resp
, srv_trans_id
);
1725 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1726 /* Response Data: empty */
1727 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1731 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1732 struct wpabuf
*resp
, u8 srv_trans_id
)
1734 struct p2p_srv_bonjour
*bsrv
;
1737 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1739 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1740 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1744 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1745 struct p2p_srv_bonjour
, list
) {
1746 if (wpabuf_tailroom(resp
) <
1747 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1749 /* Length (to be filled) */
1750 len_pos
= wpabuf_put(resp
, 2);
1751 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1752 wpabuf_put_u8(resp
, srv_trans_id
);
1754 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1755 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1756 wpabuf_head(bsrv
->resp
),
1757 wpabuf_len(bsrv
->resp
));
1759 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1760 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1761 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1767 static int match_bonjour_query(struct p2p_srv_bonjour
*bsrv
, const u8
*query
,
1770 char str_rx
[256], str_srv
[256];
1772 if (query_len
< 3 || wpabuf_len(bsrv
->query
) < 3)
1773 return 0; /* Too short to include DNS Type and Version */
1774 if (os_memcmp(query
+ query_len
- 3,
1775 wpabuf_head_u8(bsrv
->query
) + wpabuf_len(bsrv
->query
) - 3,
1777 return 0; /* Mismatch in DNS Type or Version */
1778 if (query_len
== wpabuf_len(bsrv
->query
) &&
1779 os_memcmp(query
, wpabuf_head(bsrv
->query
), query_len
- 3) == 0)
1780 return 1; /* Binary match */
1782 if (p2p_sd_dns_uncompress(str_rx
, sizeof(str_rx
), query
, query_len
- 3,
1784 return 0; /* Failed to uncompress query */
1785 if (p2p_sd_dns_uncompress(str_srv
, sizeof(str_srv
),
1786 wpabuf_head(bsrv
->query
),
1787 wpabuf_len(bsrv
->query
) - 3, 0))
1788 return 0; /* Failed to uncompress service */
1790 return os_strcmp(str_rx
, str_srv
) == 0;
1794 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1795 struct wpabuf
*resp
, u8 srv_trans_id
,
1796 const u8
*query
, size_t query_len
)
1798 struct p2p_srv_bonjour
*bsrv
;
1802 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1804 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1805 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1806 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1811 if (query_len
== 0) {
1812 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1816 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1817 struct p2p_srv_bonjour
, list
) {
1818 if (!match_bonjour_query(bsrv
, query
, query_len
))
1821 if (wpabuf_tailroom(resp
) <
1822 5 + query_len
+ wpabuf_len(bsrv
->resp
))
1827 /* Length (to be filled) */
1828 len_pos
= wpabuf_put(resp
, 2);
1829 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1830 wpabuf_put_u8(resp
, srv_trans_id
);
1833 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1834 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1835 wpabuf_head(bsrv
->resp
),
1836 wpabuf_len(bsrv
->resp
));
1839 wpabuf_put_data(resp
, query
, query_len
); /* Key */
1840 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1842 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1846 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1848 if (wpabuf_tailroom(resp
) < 5)
1851 /* Length (to be filled) */
1852 len_pos
= wpabuf_put(resp
, 2);
1853 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1854 wpabuf_put_u8(resp
, srv_trans_id
);
1857 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1858 /* Response Data: empty */
1859 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1865 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1866 struct wpabuf
*resp
, u8 srv_trans_id
)
1868 struct p2p_srv_upnp
*usrv
;
1871 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1873 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1874 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1878 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1879 struct p2p_srv_upnp
, list
) {
1880 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1883 /* Length (to be filled) */
1884 len_pos
= wpabuf_put(resp
, 2);
1885 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1886 wpabuf_put_u8(resp
, srv_trans_id
);
1889 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1891 wpabuf_put_u8(resp
, usrv
->version
);
1892 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1894 wpabuf_put_str(resp
, usrv
->service
);
1895 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1901 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1902 struct wpabuf
*resp
, u8 srv_trans_id
,
1903 const u8
*query
, size_t query_len
)
1905 struct p2p_srv_upnp
*usrv
;
1911 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1914 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1915 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1916 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1921 if (query_len
== 0) {
1922 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1926 if (wpabuf_tailroom(resp
) < 5)
1929 /* Length (to be filled) */
1930 len_pos
= wpabuf_put(resp
, 2);
1931 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1932 wpabuf_put_u8(resp
, srv_trans_id
);
1935 str
= os_malloc(query_len
);
1938 os_memcpy(str
, query
+ 1, query_len
- 1);
1939 str
[query_len
- 1] = '\0';
1941 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1942 struct p2p_srv_upnp
, list
) {
1943 if (version
!= usrv
->version
)
1946 if (os_strcmp(str
, "ssdp:all") != 0 &&
1947 os_strstr(usrv
->service
, str
) == NULL
)
1950 if (wpabuf_tailroom(resp
) < 2)
1954 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1956 wpabuf_put_u8(resp
, version
);
1958 wpabuf_put_u8(resp
, ',');
1962 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1964 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1966 wpabuf_put_str(resp
, usrv
->service
);
1971 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1974 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1975 /* Response Data: empty */
1978 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1982 #ifdef CONFIG_WIFI_DISPLAY
1983 static void wpas_sd_req_wfd(struct wpa_supplicant
*wpa_s
,
1984 struct wpabuf
*resp
, u8 srv_trans_id
,
1985 const u8
*query
, size_t query_len
)
1991 wpa_hexdump(MSG_DEBUG
, "P2P: SD Request for WFD", query
, query_len
);
1993 if (!wpa_s
->global
->wifi_display
) {
1994 wpa_printf(MSG_DEBUG
, "P2P: WFD protocol not available");
1995 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_WIFI_DISPLAY
,
2000 if (query_len
< 1) {
2001 wpa_printf(MSG_DEBUG
, "P2P: Missing WFD Requested Device "
2006 if (wpabuf_tailroom(resp
) < 5)
2011 wpa_printf(MSG_DEBUG
, "P2P: WSD for device role 0x%x", role
);
2013 /* TODO: role specific handling */
2015 /* Length (to be filled) */
2016 len_pos
= wpabuf_put(resp
, 2);
2017 wpabuf_put_u8(resp
, P2P_SERV_WIFI_DISPLAY
);
2018 wpabuf_put_u8(resp
, srv_trans_id
);
2019 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
); /* Status Code */
2021 while (pos
< query
+ query_len
) {
2022 if (*pos
< MAX_WFD_SUBELEMS
&&
2023 wpa_s
->global
->wfd_subelem
[*pos
] &&
2024 wpabuf_tailroom(resp
) >=
2025 wpabuf_len(wpa_s
->global
->wfd_subelem
[*pos
])) {
2026 wpa_printf(MSG_DEBUG
, "P2P: Add WSD response "
2027 "subelement %u", *pos
);
2028 wpabuf_put_buf(resp
, wpa_s
->global
->wfd_subelem
[*pos
]);
2033 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
2035 #endif /* CONFIG_WIFI_DISPLAY */
2038 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
2039 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
2041 struct wpa_supplicant
*wpa_s
= ctx
;
2042 const u8
*pos
= tlvs
;
2043 const u8
*end
= tlvs
+ tlvs_len
;
2046 struct wpabuf
*resp
;
2047 u8 srv_proto
, srv_trans_id
;
2051 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
2053 buf_len
= 2 * tlvs_len
+ 1;
2054 buf
= os_malloc(buf_len
);
2056 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
2057 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
2059 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
2064 if (wpa_s
->p2p_sd_over_ctrl_iface
) {
2065 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
2066 update_indic
, tlvs
, tlvs_len
);
2067 return; /* to be processed by an external program */
2070 resp
= wpabuf_alloc(10000);
2074 while (pos
+ 1 < end
) {
2075 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
2076 slen
= WPA_GET_LE16(pos
);
2078 if (pos
+ slen
> end
|| slen
< 2) {
2079 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
2084 tlv_end
= pos
+ slen
;
2087 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
2089 srv_trans_id
= *pos
++;
2090 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
2093 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
2094 pos
, tlv_end
- pos
);
2097 if (wpa_s
->force_long_sd
) {
2098 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
2100 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
2101 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
2105 switch (srv_proto
) {
2106 case P2P_SERV_ALL_SERVICES
:
2107 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
2108 "for all services");
2109 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
2110 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
2111 wpa_printf(MSG_DEBUG
, "P2P: No service "
2112 "discovery protocols available");
2113 wpas_sd_add_proto_not_avail(
2114 resp
, P2P_SERV_ALL_SERVICES
,
2118 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
2119 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
2121 case P2P_SERV_BONJOUR
:
2122 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
2123 pos
, tlv_end
- pos
);
2126 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
2127 pos
, tlv_end
- pos
);
2129 #ifdef CONFIG_WIFI_DISPLAY
2130 case P2P_SERV_WIFI_DISPLAY
:
2131 wpas_sd_req_wfd(wpa_s
, resp
, srv_trans_id
,
2132 pos
, tlv_end
- pos
);
2134 #endif /* CONFIG_WIFI_DISPLAY */
2136 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
2137 "protocol %u", srv_proto
);
2138 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
2147 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
2148 update_indic
, tlvs
, tlvs_len
);
2150 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
2156 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
2157 const u8
*tlvs
, size_t tlvs_len
)
2159 struct wpa_supplicant
*wpa_s
= ctx
;
2160 const u8
*pos
= tlvs
;
2161 const u8
*end
= tlvs
+ tlvs_len
;
2167 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
2169 if (tlvs_len
> 1500) {
2170 /* TODO: better way for handling this */
2171 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
2172 P2P_EVENT_SERV_DISC_RESP MACSTR
2173 " %u <long response: %u bytes>",
2174 MAC2STR(sa
), update_indic
,
2175 (unsigned int) tlvs_len
);
2177 buf_len
= 2 * tlvs_len
+ 1;
2178 buf
= os_malloc(buf_len
);
2180 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
2181 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
2182 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
2183 MAC2STR(sa
), update_indic
, buf
);
2189 u8 srv_proto
, srv_trans_id
, status
;
2191 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
2192 slen
= WPA_GET_LE16(pos
);
2194 if (pos
+ slen
> end
|| slen
< 3) {
2195 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
2199 tlv_end
= pos
+ slen
;
2202 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
2204 srv_trans_id
= *pos
++;
2205 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
2208 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
2211 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
2212 pos
, tlv_end
- pos
);
2217 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
2221 u64
wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2222 const struct wpabuf
*tlvs
)
2224 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2225 return wpa_drv_p2p_sd_request(wpa_s
, dst
, tlvs
);
2226 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2228 return (uintptr_t) p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
2232 u64
wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2233 u8 version
, const char *query
)
2235 struct wpabuf
*tlvs
;
2238 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
2241 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
2242 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
2243 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
2244 wpabuf_put_u8(tlvs
, version
);
2245 wpabuf_put_str(tlvs
, query
);
2246 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
2252 #ifdef CONFIG_WIFI_DISPLAY
2254 static u64
wpas_p2p_sd_request_wfd(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2255 const struct wpabuf
*tlvs
)
2257 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2259 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2261 return (uintptr_t) p2p_sd_request_wfd(wpa_s
->global
->p2p
, dst
, tlvs
);
2265 #define MAX_WFD_SD_SUBELEMS 20
2267 static void wfd_add_sd_req_role(struct wpabuf
*tlvs
, u8 id
, u8 role
,
2268 const char *subelems
)
2275 len
= wpabuf_put(tlvs
, 2);
2276 wpabuf_put_u8(tlvs
, P2P_SERV_WIFI_DISPLAY
); /* Service Protocol Type */
2277 wpabuf_put_u8(tlvs
, id
); /* Service Transaction ID */
2279 wpabuf_put_u8(tlvs
, role
);
2284 if (val
>= 0 && val
< 256) {
2285 wpabuf_put_u8(tlvs
, val
);
2287 if (count
== MAX_WFD_SD_SUBELEMS
)
2290 pos
= os_strchr(pos
+ 1, ',');
2296 WPA_PUT_LE16(len
, (u8
*) wpabuf_put(tlvs
, 0) - len
- 2);
2300 u64
wpas_p2p_sd_request_wifi_display(struct wpa_supplicant
*wpa_s
,
2301 const u8
*dst
, const char *role
)
2303 struct wpabuf
*tlvs
;
2305 const char *subelems
;
2308 subelems
= os_strchr(role
, ' ');
2309 if (subelems
== NULL
)
2313 tlvs
= wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS
));
2317 if (os_strstr(role
, "[source]"))
2318 wfd_add_sd_req_role(tlvs
, id
++, 0x00, subelems
);
2319 if (os_strstr(role
, "[pri-sink]"))
2320 wfd_add_sd_req_role(tlvs
, id
++, 0x01, subelems
);
2321 if (os_strstr(role
, "[sec-sink]"))
2322 wfd_add_sd_req_role(tlvs
, id
++, 0x02, subelems
);
2323 if (os_strstr(role
, "[source+sink]"))
2324 wfd_add_sd_req_role(tlvs
, id
++, 0x03, subelems
);
2326 ret
= wpas_p2p_sd_request_wfd(wpa_s
, dst
, tlvs
);
2331 #endif /* CONFIG_WIFI_DISPLAY */
2334 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, u64 req
)
2336 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2337 return wpa_drv_p2p_sd_cancel_request(wpa_s
, req
);
2338 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2340 return p2p_sd_cancel_request(wpa_s
->global
->p2p
,
2341 (void *) (uintptr_t) req
);
2345 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
2346 const u8
*dst
, u8 dialog_token
,
2347 const struct wpabuf
*resp_tlvs
)
2349 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2350 wpa_drv_p2p_sd_response(wpa_s
, freq
, dst
, dialog_token
,
2354 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2356 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
2361 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
2363 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2364 wpa_drv_p2p_service_update(wpa_s
);
2367 if (wpa_s
->global
->p2p
)
2368 p2p_sd_service_update(wpa_s
->global
->p2p
);
2372 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
2374 dl_list_del(&bsrv
->list
);
2375 wpabuf_free(bsrv
->query
);
2376 wpabuf_free(bsrv
->resp
);
2381 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
2383 dl_list_del(&usrv
->list
);
2384 os_free(usrv
->service
);
2389 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
2391 struct p2p_srv_bonjour
*bsrv
, *bn
;
2392 struct p2p_srv_upnp
*usrv
, *un
;
2394 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
2395 struct p2p_srv_bonjour
, list
)
2396 wpas_p2p_srv_bonjour_free(bsrv
);
2398 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
2399 struct p2p_srv_upnp
, list
)
2400 wpas_p2p_srv_upnp_free(usrv
);
2402 wpas_p2p_sd_service_update(wpa_s
);
2406 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
2407 struct wpabuf
*query
, struct wpabuf
*resp
)
2409 struct p2p_srv_bonjour
*bsrv
;
2411 bsrv
= os_zalloc(sizeof(*bsrv
));
2414 bsrv
->query
= query
;
2416 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
2418 wpas_p2p_sd_service_update(wpa_s
);
2423 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
2424 const struct wpabuf
*query
)
2426 struct p2p_srv_bonjour
*bsrv
;
2428 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
2431 wpas_p2p_srv_bonjour_free(bsrv
);
2432 wpas_p2p_sd_service_update(wpa_s
);
2437 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2438 const char *service
)
2440 struct p2p_srv_upnp
*usrv
;
2442 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
2443 return 0; /* Already listed */
2444 usrv
= os_zalloc(sizeof(*usrv
));
2447 usrv
->version
= version
;
2448 usrv
->service
= os_strdup(service
);
2449 if (usrv
->service
== NULL
) {
2453 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
2455 wpas_p2p_sd_service_update(wpa_s
);
2460 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2461 const char *service
)
2463 struct p2p_srv_upnp
*usrv
;
2465 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
2468 wpas_p2p_srv_upnp_free(usrv
);
2469 wpas_p2p_sd_service_update(wpa_s
);
2474 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
2475 const u8
*peer
, const char *params
,
2476 unsigned int generated_pin
)
2478 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2479 " %08d%s", MAC2STR(peer
), generated_pin
, params
);
2483 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
2484 const u8
*peer
, const char *params
)
2486 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2487 "%s", MAC2STR(peer
), params
);
2491 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
2492 const u8
*dev_addr
, const u8
*pri_dev_type
,
2493 const char *dev_name
, u16 supp_config_methods
,
2494 u8 dev_capab
, u8 group_capab
, const u8
*group_id
,
2495 size_t group_id_len
)
2497 struct wpa_supplicant
*wpa_s
= ctx
;
2498 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
2500 u8 empty_dev_type
[8];
2501 unsigned int generated_pin
= 0;
2502 struct wpa_supplicant
*group
= NULL
;
2505 for (group
= wpa_s
->global
->ifaces
; group
; group
= group
->next
)
2507 struct wpa_ssid
*s
= group
->current_ssid
;
2509 s
->mode
== WPAS_MODE_P2P_GO
&&
2510 group_id_len
- ETH_ALEN
== s
->ssid_len
&&
2511 os_memcmp(group_id
+ ETH_ALEN
, s
->ssid
,
2517 if (pri_dev_type
== NULL
) {
2518 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
2519 pri_dev_type
= empty_dev_type
;
2521 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
2522 " pri_dev_type=%s name='%s' config_methods=0x%x "
2523 "dev_capab=0x%x group_capab=0x%x%s%s",
2525 wps_dev_type_bin2str(pri_dev_type
, devtype
,
2527 dev_name
, supp_config_methods
, dev_capab
, group_capab
,
2528 group
? " group=" : "",
2529 group
? group
->ifname
: "");
2530 params
[sizeof(params
) - 1] = '\0';
2532 if (config_methods
& WPS_CONFIG_DISPLAY
) {
2533 generated_pin
= wps_generate_pin();
2534 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
2536 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
2537 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
2538 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
2539 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ
2540 MACSTR
"%s", MAC2STR(peer
), params
);
2542 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 1 /* request */,
2543 P2P_PROV_DISC_SUCCESS
,
2544 config_methods
, generated_pin
);
2548 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
2550 struct wpa_supplicant
*wpa_s
= ctx
;
2551 unsigned int generated_pin
= 0;
2554 if (wpa_s
->pending_pd_before_join
&&
2555 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
2556 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
2557 wpa_s
->pending_pd_before_join
= 0;
2558 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
2559 "join-existing-group operation");
2560 wpas_p2p_join_start(wpa_s
);
2564 if (wpa_s
->pending_pd_use
== AUTO_PD_JOIN
||
2565 wpa_s
->pending_pd_use
== AUTO_PD_GO_NEG
)
2566 os_snprintf(params
, sizeof(params
), " peer_go=%d",
2567 wpa_s
->pending_pd_use
== AUTO_PD_JOIN
);
2571 if (config_methods
& WPS_CONFIG_DISPLAY
)
2572 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
2573 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
2574 generated_pin
= wps_generate_pin();
2575 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
2577 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
2578 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP
2579 MACSTR
"%s", MAC2STR(peer
), params
);
2581 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
2582 P2P_PROV_DISC_SUCCESS
,
2583 config_methods
, generated_pin
);
2587 static void wpas_prov_disc_fail(void *ctx
, const u8
*peer
,
2588 enum p2p_prov_disc_status status
)
2590 struct wpa_supplicant
*wpa_s
= ctx
;
2592 if (wpa_s
->p2p_fallback_to_go_neg
) {
2593 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: PD for p2p_connect-auto "
2594 "failed - fall back to GO Negotiation");
2595 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
2599 if (status
== P2P_PROV_DISC_TIMEOUT_JOIN
) {
2600 wpa_s
->pending_pd_before_join
= 0;
2601 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
2602 "join-existing-group operation (no ACK for PD "
2604 wpas_p2p_join_start(wpa_s
);
2608 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
2609 " p2p_dev_addr=" MACSTR
" status=%d",
2610 MAC2STR(peer
), status
);
2612 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
2617 static int freq_included(const struct p2p_channels
*channels
, unsigned int freq
)
2619 if (channels
== NULL
)
2620 return 1; /* Assume no restrictions */
2621 return p2p_channels_includes_freq(channels
, freq
);
2626 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
2627 const u8
*go_dev_addr
, const u8
*ssid
,
2628 size_t ssid_len
, int *go
, u8
*group_bssid
,
2629 int *force_freq
, int persistent_group
,
2630 const struct p2p_channels
*channels
)
2632 struct wpa_supplicant
*wpa_s
= ctx
;
2635 struct wpa_supplicant
*grp
;
2637 if (!persistent_group
) {
2638 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
2639 " to join an active group", MAC2STR(sa
));
2640 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
2641 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
2643 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
2644 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
2645 "authorized invitation");
2649 * Do not accept the invitation automatically; notify user and
2652 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
2655 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
2657 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
2658 "running persistent group");
2660 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
2664 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
2665 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0) {
2666 wpa_printf(MSG_DEBUG
, "P2P: Accept previously initiated "
2667 "invitation to re-invoke a persistent group");
2668 } else if (!wpa_s
->conf
->persistent_reconnect
)
2669 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
2671 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2672 if (s
->disabled
== 2 &&
2673 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
2674 s
->ssid_len
== ssid_len
&&
2675 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2680 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
2681 " requested reinvocation of an unknown group",
2683 return P2P_SC_FAIL_UNKNOWN_GROUP
;
2686 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
2688 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
2689 wpa_printf(MSG_DEBUG
, "P2P: The only available "
2690 "interface is already in use - reject "
2692 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
2694 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
2695 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
2697 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
2699 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2700 "interface address for the group");
2701 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
2703 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
2708 wpas_p2p_set_own_freq_preference(wpa_s
, 0);
2710 /* Get one of the frequencies currently in use */
2711 if (wpas_p2p_valid_oper_freqs(wpa_s
, &res
, 1) > 0) {
2712 wpa_printf(MSG_DEBUG
, "P2P: Trying to prefer a channel already used by one of the interfaces");
2713 wpas_p2p_set_own_freq_preference(wpa_s
, res
);
2715 if (wpa_s
->num_multichan_concurrent
< 2 ||
2716 wpas_p2p_num_unused_channels(wpa_s
) < 1) {
2717 wpa_printf(MSG_DEBUG
, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2722 if (*force_freq
> 0 && wpa_s
->num_multichan_concurrent
> 1 &&
2723 wpas_p2p_num_unused_channels(wpa_s
) > 0) {
2725 /* We are the client */
2726 wpa_printf(MSG_DEBUG
, "P2P: Peer was found to be "
2727 "running a GO but we are capable of MCC, "
2728 "figure out the best channel to use");
2730 } else if (!freq_included(channels
, *force_freq
)) {
2731 /* We are the GO, and *force_freq is not in the
2733 wpa_printf(MSG_DEBUG
, "P2P: Forced GO freq %d MHz not "
2734 "in intersection but we are capable of MCC, "
2735 "figure out the best channel to use",
2741 return P2P_SC_SUCCESS
;
2745 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
2746 const u8
*ssid
, size_t ssid_len
,
2747 const u8
*go_dev_addr
, u8 status
,
2750 struct wpa_supplicant
*wpa_s
= ctx
;
2753 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2754 if (s
->disabled
== 2 &&
2755 s
->ssid_len
== ssid_len
&&
2756 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2760 if (status
== P2P_SC_SUCCESS
) {
2761 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2762 " was accepted; op_freq=%d MHz",
2763 MAC2STR(sa
), op_freq
);
2765 int go
= s
->mode
== WPAS_MODE_P2P_GO
;
2766 wpas_p2p_group_add_persistent(
2767 wpa_s
, s
, go
, go
? op_freq
: 0, 0, 0, NULL
,
2768 go
? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
: 0);
2770 wpa_s
->user_initiated_pd
= 0;
2771 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
2772 wpa_s
->p2p_wps_method
, 0);
2777 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2778 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2779 " was rejected (status %u)", MAC2STR(sa
), status
);
2785 wpa_msg_global(wpa_s
, MSG_INFO
,
2786 P2P_EVENT_INVITATION_RECEIVED
2787 "sa=" MACSTR
" go_dev_addr=" MACSTR
2788 " bssid=" MACSTR
" unknown-network",
2789 MAC2STR(sa
), MAC2STR(go_dev_addr
),
2792 wpa_msg_global(wpa_s
, MSG_INFO
,
2793 P2P_EVENT_INVITATION_RECEIVED
2794 "sa=" MACSTR
" go_dev_addr=" MACSTR
2796 MAC2STR(sa
), MAC2STR(go_dev_addr
));
2801 if (s
->mode
== WPAS_MODE_P2P_GO
&& op_freq
) {
2802 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2803 "sa=" MACSTR
" persistent=%d freq=%d",
2804 MAC2STR(sa
), s
->id
, op_freq
);
2806 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2807 "sa=" MACSTR
" persistent=%d",
2808 MAC2STR(sa
), s
->id
);
2813 static void wpas_remove_persistent_peer(struct wpa_supplicant
*wpa_s
,
2814 struct wpa_ssid
*ssid
,
2815 const u8
*peer
, int inv
)
2822 for (i
= 0; ssid
->p2p_client_list
&& i
< ssid
->num_p2p_clients
; i
++) {
2823 if (os_memcmp(ssid
->p2p_client_list
+ i
* ETH_ALEN
, peer
,
2827 if (i
>= ssid
->num_p2p_clients
) {
2828 if (ssid
->mode
!= WPAS_MODE_P2P_GO
&&
2829 os_memcmp(ssid
->bssid
, peer
, ETH_ALEN
) == 0) {
2830 wpa_printf(MSG_DEBUG
, "P2P: Remove persistent group %d "
2831 "due to invitation result", ssid
->id
);
2832 wpas_notify_network_removed(wpa_s
, ssid
);
2833 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
2836 return; /* Peer not found in client list */
2839 wpa_printf(MSG_DEBUG
, "P2P: Remove peer " MACSTR
" from persistent "
2840 "group %d client list%s",
2841 MAC2STR(peer
), ssid
->id
,
2842 inv
? " due to invitation result" : "");
2843 os_memmove(ssid
->p2p_client_list
+ i
* ETH_ALEN
,
2844 ssid
->p2p_client_list
+ (i
+ 1) * ETH_ALEN
,
2845 (ssid
->num_p2p_clients
- i
- 1) * ETH_ALEN
);
2846 ssid
->num_p2p_clients
--;
2847 #ifndef CONFIG_NO_CONFIG_WRITE
2848 if (wpa_s
->parent
->conf
->update_config
&&
2849 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
2850 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
2851 #endif /* CONFIG_NO_CONFIG_WRITE */
2855 static void wpas_remove_persistent_client(struct wpa_supplicant
*wpa_s
,
2858 struct wpa_ssid
*ssid
;
2860 wpa_s
= wpa_s
->global
->p2p_invite_group
;
2862 return; /* No known invitation group */
2863 ssid
= wpa_s
->current_ssid
;
2864 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
2865 !ssid
->p2p_persistent_group
)
2866 return; /* Not operating as a GO in persistent group */
2867 ssid
= wpas_p2p_get_persistent(wpa_s
->parent
, peer
,
2868 ssid
->ssid
, ssid
->ssid_len
);
2869 wpas_remove_persistent_peer(wpa_s
, ssid
, peer
, 1);
2873 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
,
2874 const struct p2p_channels
*channels
,
2875 const u8
*peer
, int neg_freq
)
2877 struct wpa_supplicant
*wpa_s
= ctx
;
2878 struct wpa_ssid
*ssid
;
2882 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2883 "status=%d " MACSTR
,
2884 status
, MAC2STR(bssid
));
2886 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2887 "status=%d ", status
);
2889 wpas_notify_p2p_invitation_result(wpa_s
, status
, bssid
);
2891 wpa_printf(MSG_DEBUG
, "P2P: Invitation result - status=%d peer=" MACSTR
,
2892 status
, MAC2STR(peer
));
2893 if (wpa_s
->pending_invite_ssid_id
== -1) {
2894 if (status
== P2P_SC_FAIL_UNKNOWN_GROUP
)
2895 wpas_remove_persistent_client(wpa_s
, peer
);
2896 return; /* Invitation to active group */
2899 if (status
== P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2900 wpa_printf(MSG_DEBUG
, "P2P: Waiting for peer to start another "
2901 "invitation exchange to indicate readiness for "
2905 if (status
!= P2P_SC_SUCCESS
) {
2906 if (status
== P2P_SC_FAIL_UNKNOWN_GROUP
) {
2907 ssid
= wpa_config_get_network(
2908 wpa_s
->conf
, wpa_s
->pending_invite_ssid_id
);
2909 wpas_remove_persistent_peer(wpa_s
, ssid
, peer
, 1);
2911 wpas_p2p_remove_pending_group_interface(wpa_s
);
2915 ssid
= wpa_config_get_network(wpa_s
->conf
,
2916 wpa_s
->pending_invite_ssid_id
);
2918 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
2919 "data matching with invitation");
2924 * The peer could have missed our ctrl::ack frame for Invitation
2925 * Response and continue retransmitting the frame. To reduce the
2926 * likelihood of the peer not getting successful TX status for the
2927 * Invitation Response frame, wait a short time here before starting
2928 * the persistent group so that we will remain on the current channel to
2929 * acknowledge any possible retransmission from the peer.
2931 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: 50 ms wait on current channel before "
2932 "starting persistent group");
2935 freq
= wpa_s
->p2p_persistent_go_freq
;
2936 if (neg_freq
> 0 && ssid
->mode
== WPAS_MODE_P2P_GO
&&
2937 freq_included(channels
, neg_freq
)) {
2938 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use frequence %d MHz from invitation for GO mode",
2943 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
2944 ssid
->mode
== WPAS_MODE_P2P_GO
,
2946 wpa_s
->p2p_go_ht40
, wpa_s
->p2p_go_vht
,
2948 ssid
->mode
== WPAS_MODE_P2P_GO
?
2949 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
:
2954 static int wpas_p2p_disallowed_freq(struct wpa_global
*global
,
2957 return freq_range_list_includes(&global
->p2p_disallow_freq
, freq
);
2961 static void wpas_p2p_add_chan(struct p2p_reg_class
*reg
, u8 chan
)
2963 reg
->channel
[reg
->channels
] = chan
;
2968 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
2969 struct p2p_channels
*chan
,
2970 struct p2p_channels
*cli_chan
)
2974 os_memset(cli_chan
, 0, sizeof(*cli_chan
));
2976 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
2979 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2980 chan
->reg_class
[cla
].reg_class
= 81;
2981 chan
->reg_class
[cla
].channels
= 0;
2982 for (i
= 0; i
< 11; i
++) {
2983 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 2412 + i
* 5))
2984 wpas_p2p_add_chan(&chan
->reg_class
[cla
], i
+ 1);
2986 if (chan
->reg_class
[cla
].channels
)
2989 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
2992 /* Operating class 115 - 5 GHz, channels 36-48 */
2993 chan
->reg_class
[cla
].reg_class
= 115;
2994 chan
->reg_class
[cla
].channels
= 0;
2995 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 36 * 5))
2996 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 36);
2997 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 40 * 5))
2998 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 40);
2999 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 44 * 5))
3000 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 44);
3001 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 48 * 5))
3002 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 48);
3003 if (chan
->reg_class
[cla
].channels
)
3006 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
3009 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3010 chan
->reg_class
[cla
].reg_class
= 124;
3011 chan
->reg_class
[cla
].channels
= 0;
3012 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 149 * 5))
3013 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 149);
3014 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 153 * 5))
3015 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 153);
3016 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 156 * 5))
3017 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 157);
3018 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 161 * 5))
3019 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 161);
3020 if (chan
->reg_class
[cla
].channels
)
3023 chan
->reg_classes
= cla
;
3028 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
3030 enum hostapd_hw_mode mode
)
3034 for (i
= 0; i
< num_modes
; i
++) {
3035 if (modes
[i
].mode
== mode
)
3044 NOT_ALLOWED
, PASSIVE_ONLY
, ALLOWED
3047 static int has_channel(struct wpa_global
*global
,
3048 struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
3053 freq
= (mode
->mode
== HOSTAPD_MODE_IEEE80211A
? 5000 : 2407) +
3055 if (wpas_p2p_disallowed_freq(global
, freq
))
3058 for (i
= 0; i
< mode
->num_channels
; i
++) {
3059 if (mode
->channels
[i
].chan
== chan
) {
3061 *flags
= mode
->channels
[i
].flag
;
3062 if (mode
->channels
[i
].flag
&
3063 (HOSTAPD_CHAN_DISABLED
|
3064 HOSTAPD_CHAN_RADAR
))
3066 if (mode
->channels
[i
].flag
&
3067 (HOSTAPD_CHAN_PASSIVE_SCAN
|
3068 HOSTAPD_CHAN_NO_IBSS
))
3069 return PASSIVE_ONLY
;
3078 struct p2p_oper_class_map
{
3079 enum hostapd_hw_mode mode
;
3084 enum { BW20
, BW40PLUS
, BW40MINUS
, BW80
} bw
;
3087 static struct p2p_oper_class_map op_class
[] = {
3088 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
3089 #if 0 /* Do not enable HT40 on 2 GHz for now */
3090 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
3091 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
3093 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
3094 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
3095 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
3096 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
3097 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
3098 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
3101 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3102 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3103 * 80 MHz, but currently use the following definition for simplicity
3104 * (these center frequencies are not actual channels, which makes
3105 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3106 * removing invalid channels.
3108 { HOSTAPD_MODE_IEEE80211A
, 128, 36, 161, 4, BW80
},
3109 { -1, 0, 0, 0, 0, BW20
}
3113 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant
*wpa_s
,
3114 struct hostapd_hw_modes
*mode
,
3117 u8 center_channels
[] = { 42, 58, 106, 122, 138, 155 };
3120 if (mode
->mode
!= HOSTAPD_MODE_IEEE80211A
)
3123 for (i
= 0; i
< ARRAY_SIZE(center_channels
); i
++)
3125 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3126 * so the center channel is 6 channels away from the start/end.
3128 if (channel
>= center_channels
[i
] - 6 &&
3129 channel
<= center_channels
[i
] + 6)
3130 return center_channels
[i
];
3136 static enum chan_allowed
wpas_p2p_verify_80mhz(struct wpa_supplicant
*wpa_s
,
3137 struct hostapd_hw_modes
*mode
,
3142 enum chan_allowed res
, ret
= ALLOWED
;
3144 center_chan
= wpas_p2p_get_center_80mhz(wpa_s
, mode
, channel
);
3147 if (center_chan
>= 58 && center_chan
<= 138)
3148 return NOT_ALLOWED
; /* Do not allow DFS channels for P2P */
3150 /* check all the channels are available */
3151 for (i
= 0; i
< 4; i
++) {
3152 int adj_chan
= center_chan
- 6 + i
* 4;
3154 res
= has_channel(wpa_s
->global
, mode
, adj_chan
, &flags
);
3155 if (res
== NOT_ALLOWED
)
3157 if (res
== PASSIVE_ONLY
)
3160 if (i
== 0 && !(flags
& HOSTAPD_CHAN_VHT_10_70
))
3162 if (i
== 1 && !(flags
& HOSTAPD_CHAN_VHT_30_50
))
3164 if (i
== 2 && !(flags
& HOSTAPD_CHAN_VHT_50_30
))
3166 if (i
== 3 && !(flags
& HOSTAPD_CHAN_VHT_70_10
))
3174 static enum chan_allowed
wpas_p2p_verify_channel(struct wpa_supplicant
*wpa_s
,
3175 struct hostapd_hw_modes
*mode
,
3179 enum chan_allowed res
, res2
;
3181 res2
= res
= has_channel(wpa_s
->global
, mode
, channel
, &flag
);
3182 if (bw
== BW40MINUS
) {
3183 if (!(flag
& HOSTAPD_CHAN_HT40MINUS
))
3185 res2
= has_channel(wpa_s
->global
, mode
, channel
- 4, NULL
);
3186 } else if (bw
== BW40PLUS
) {
3187 if (!(flag
& HOSTAPD_CHAN_HT40PLUS
))
3189 res2
= has_channel(wpa_s
->global
, mode
, channel
+ 4, NULL
);
3190 } else if (bw
== BW80
) {
3191 res2
= wpas_p2p_verify_80mhz(wpa_s
, mode
, channel
, bw
);
3194 if (res
== NOT_ALLOWED
|| res2
== NOT_ALLOWED
)
3196 if (res
== PASSIVE_ONLY
|| res2
== PASSIVE_ONLY
)
3197 return PASSIVE_ONLY
;
3202 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
3203 struct p2p_channels
*chan
,
3204 struct p2p_channels
*cli_chan
)
3206 struct hostapd_hw_modes
*mode
;
3207 int cla
, op
, cli_cla
;
3209 if (wpa_s
->hw
.modes
== NULL
) {
3210 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
3211 "of all supported channels; assume dualband "
3213 return wpas_p2p_default_channels(wpa_s
, chan
, cli_chan
);
3218 for (op
= 0; op_class
[op
].op_class
; op
++) {
3219 struct p2p_oper_class_map
*o
= &op_class
[op
];
3221 struct p2p_reg_class
*reg
= NULL
, *cli_reg
= NULL
;
3223 mode
= get_mode(wpa_s
->hw
.modes
, wpa_s
->hw
.num_modes
, o
->mode
);
3226 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
3227 enum chan_allowed res
;
3228 res
= wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
);
3229 if (res
== ALLOWED
) {
3231 wpa_printf(MSG_DEBUG
, "P2P: Add operating class %u",
3233 reg
= &chan
->reg_class
[cla
];
3235 reg
->reg_class
= o
->op_class
;
3237 reg
->channel
[reg
->channels
] = ch
;
3239 } else if (res
== PASSIVE_ONLY
&&
3240 wpa_s
->conf
->p2p_add_cli_chan
) {
3241 if (cli_reg
== NULL
) {
3242 wpa_printf(MSG_DEBUG
, "P2P: Add operating class %u (client only)",
3244 cli_reg
= &cli_chan
->reg_class
[cli_cla
];
3246 cli_reg
->reg_class
= o
->op_class
;
3248 cli_reg
->channel
[cli_reg
->channels
] = ch
;
3249 cli_reg
->channels
++;
3253 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
3254 reg
->channel
, reg
->channels
);
3257 wpa_hexdump(MSG_DEBUG
, "P2P: Channels (client only)",
3258 cli_reg
->channel
, cli_reg
->channels
);
3262 chan
->reg_classes
= cla
;
3263 cli_chan
->reg_classes
= cli_cla
;
3269 int wpas_p2p_get_ht40_mode(struct wpa_supplicant
*wpa_s
,
3270 struct hostapd_hw_modes
*mode
, u8 channel
)
3273 enum chan_allowed ret
;
3275 for (op
= 0; op_class
[op
].op_class
; op
++) {
3276 struct p2p_oper_class_map
*o
= &op_class
[op
];
3279 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
3280 if (o
->mode
!= HOSTAPD_MODE_IEEE80211A
||
3281 o
->bw
== BW20
|| ch
!= channel
)
3283 ret
= wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
);
3285 return (o
->bw
== BW40MINUS
) ? -1 : 1;
3292 int wpas_p2p_get_vht80_center(struct wpa_supplicant
*wpa_s
,
3293 struct hostapd_hw_modes
*mode
, u8 channel
)
3295 if (!wpas_p2p_verify_channel(wpa_s
, mode
, channel
, BW80
))
3298 return wpas_p2p_get_center_80mhz(wpa_s
, mode
, channel
);
3302 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
3305 struct wpa_supplicant
*wpa_s
= ctx
;
3307 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3308 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
3314 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
3318 static int wpas_go_connected(void *ctx
, const u8
*dev_addr
)
3320 struct wpa_supplicant
*wpa_s
= ctx
;
3322 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3323 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
3326 if (ssid
->mode
!= WPAS_MODE_INFRA
)
3328 if (wpa_s
->wpa_state
!= WPA_COMPLETED
&&
3329 wpa_s
->wpa_state
!= WPA_GROUP_HANDSHAKE
)
3331 if (os_memcmp(wpa_s
->go_dev_addr
, dev_addr
, ETH_ALEN
) == 0)
3339 static void wpas_p2p_debug_print(void *ctx
, int level
, const char *msg
)
3341 struct wpa_supplicant
*wpa_s
= ctx
;
3342 wpa_msg_global(wpa_s
, level
, "P2P: %s", msg
);
3346 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant
*wpa_s
)
3348 struct wpa_interface iface
;
3349 struct wpa_supplicant
*p2pdev_wpa_s
;
3351 char force_name
[100];
3354 os_snprintf(ifname
, sizeof(ifname
), P2P_MGMT_DEVICE_PREFIX
"%s",
3356 force_name
[0] = '\0';
3357 wpa_s
->pending_interface_type
= WPA_IF_P2P_DEVICE
;
3358 ret
= wpa_drv_if_add(wpa_s
, WPA_IF_P2P_DEVICE
, ifname
, NULL
, NULL
,
3359 force_name
, wpa_s
->pending_interface_addr
, NULL
);
3361 wpa_printf(MSG_DEBUG
, "P2P: Failed to create P2P Device interface");
3364 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
3365 sizeof(wpa_s
->pending_interface_name
));
3367 os_memset(&iface
, 0, sizeof(iface
));
3369 iface
.ifname
= wpa_s
->pending_interface_name
;
3370 iface
.driver
= wpa_s
->driver
->name
;
3371 iface
.driver_param
= wpa_s
->conf
->driver_param
;
3372 iface
.confname
= wpa_s
->confname
;
3373 p2pdev_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
3374 if (!p2pdev_wpa_s
) {
3375 wpa_printf(MSG_DEBUG
, "P2P: Failed to add P2P Device interface");
3378 p2pdev_wpa_s
->parent
= wpa_s
;
3380 wpa_s
->pending_interface_name
[0] = '\0';
3386 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3387 * @global: Pointer to global data from wpa_supplicant_init()
3388 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3389 * Returns: 0 on success, -1 on failure
3391 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
3393 struct p2p_config p2p
;
3397 if (wpa_s
->conf
->p2p_disabled
)
3400 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
3406 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3407 struct p2p_params params
;
3409 wpa_printf(MSG_DEBUG
, "P2P: Use driver-based P2P management");
3410 os_memset(¶ms
, 0, sizeof(params
));
3411 params
.dev_name
= wpa_s
->conf
->device_name
;
3412 os_memcpy(params
.pri_dev_type
, wpa_s
->conf
->device_type
,
3414 params
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
3415 os_memcpy(params
.sec_dev_type
,
3416 wpa_s
->conf
->sec_device_type
,
3417 params
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
3419 if (wpa_drv_p2p_set_params(wpa_s
, ¶ms
) < 0)
3425 os_memset(&p2p
, 0, sizeof(p2p
));
3427 p2p
.debug_print
= wpas_p2p_debug_print
;
3428 p2p
.p2p_scan
= wpas_p2p_scan
;
3429 p2p
.send_action
= wpas_send_action
;
3430 p2p
.send_action_done
= wpas_send_action_done
;
3431 p2p
.go_neg_completed
= wpas_go_neg_completed
;
3432 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
3433 p2p
.dev_found
= wpas_dev_found
;
3434 p2p
.dev_lost
= wpas_dev_lost
;
3435 p2p
.find_stopped
= wpas_find_stopped
;
3436 p2p
.start_listen
= wpas_start_listen
;
3437 p2p
.stop_listen
= wpas_stop_listen
;
3438 p2p
.send_probe_resp
= wpas_send_probe_resp
;
3439 p2p
.sd_request
= wpas_sd_request
;
3440 p2p
.sd_response
= wpas_sd_response
;
3441 p2p
.prov_disc_req
= wpas_prov_disc_req
;
3442 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
3443 p2p
.prov_disc_fail
= wpas_prov_disc_fail
;
3444 p2p
.invitation_process
= wpas_invitation_process
;
3445 p2p
.invitation_received
= wpas_invitation_received
;
3446 p2p
.invitation_result
= wpas_invitation_result
;
3447 p2p
.get_noa
= wpas_get_noa
;
3448 p2p
.go_connected
= wpas_go_connected
;
3450 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
3451 os_memcpy(p2p
.dev_addr
, wpa_s
->global
->p2p_dev_addr
, ETH_ALEN
);
3452 p2p
.dev_name
= wpa_s
->conf
->device_name
;
3453 p2p
.manufacturer
= wpa_s
->conf
->manufacturer
;
3454 p2p
.model_name
= wpa_s
->conf
->model_name
;
3455 p2p
.model_number
= wpa_s
->conf
->model_number
;
3456 p2p
.serial_number
= wpa_s
->conf
->serial_number
;
3458 os_memcpy(p2p
.uuid
, wpa_s
->wps
->uuid
, 16);
3459 p2p
.config_methods
= wpa_s
->wps
->config_methods
;
3462 if (wpa_s
->conf
->p2p_listen_reg_class
&&
3463 wpa_s
->conf
->p2p_listen_channel
) {
3464 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
3465 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
3469 * Pick one of the social channels randomly as the listen
3472 os_get_random((u8
*) &r
, sizeof(r
));
3473 p2p
.channel
= 1 + (r
% 3) * 5;
3475 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
3477 if (wpa_s
->conf
->p2p_oper_reg_class
&&
3478 wpa_s
->conf
->p2p_oper_channel
) {
3479 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
3480 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
3481 p2p
.cfg_op_channel
= 1;
3482 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
3483 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
3486 p2p
.op_reg_class
= 81;
3488 * Use random operation channel from (1, 6, 11) if no other
3489 * preference is indicated.
3491 os_get_random((u8
*) &r
, sizeof(r
));
3492 p2p
.op_channel
= 1 + (r
% 3) * 5;
3493 p2p
.cfg_op_channel
= 0;
3494 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
3495 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
3498 if (wpa_s
->conf
->p2p_pref_chan
&& wpa_s
->conf
->num_p2p_pref_chan
) {
3499 p2p
.pref_chan
= wpa_s
->conf
->p2p_pref_chan
;
3500 p2p
.num_pref_chan
= wpa_s
->conf
->num_p2p_pref_chan
;
3503 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
3504 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
3505 p2p
.country
[2] = 0x04;
3507 os_memcpy(p2p
.country
, "XX\x04", 3);
3509 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
, &p2p
.cli_channels
)) {
3510 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
3515 os_memcpy(p2p
.pri_dev_type
, wpa_s
->conf
->device_type
,
3518 p2p
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
3519 os_memcpy(p2p
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
3520 p2p
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
3522 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
3523 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
3525 p2p
.max_peers
= 100;
3527 if (wpa_s
->conf
->p2p_ssid_postfix
) {
3528 p2p
.ssid_postfix_len
=
3529 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
3530 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
3531 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
3532 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
3533 p2p
.ssid_postfix_len
);
3536 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
3538 p2p
.max_listen
= wpa_s
->max_remain_on_chan
;
3540 global
->p2p
= p2p_init(&p2p
);
3541 if (global
->p2p
== NULL
)
3543 global
->p2p_init_wpa_s
= wpa_s
;
3545 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
3546 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
3548 p2p_add_wps_vendor_extension(
3549 global
->p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
3552 p2p_set_no_go_freq(global
->p2p
, &wpa_s
->conf
->p2p_no_go_freq
);
3559 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3560 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3562 * This function deinitialize per-interface P2P data.
3564 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
3566 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
3567 wpa_drv_probe_req_report(wpa_s
, 0);
3569 if (wpa_s
->go_params
) {
3570 /* Clear any stored provisioning info */
3571 p2p_clear_provisioning_info(
3573 wpa_s
->go_params
->peer_device_addr
);
3576 os_free(wpa_s
->go_params
);
3577 wpa_s
->go_params
= NULL
;
3578 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
3579 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3580 wpa_s
->p2p_long_listen
= 0;
3581 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3582 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3583 wpas_p2p_remove_pending_group_interface(wpa_s
);
3584 eloop_cancel_timeout(wpas_p2p_group_freq_conflict
, wpa_s
, NULL
);
3586 /* TODO: remove group interface from the driver if this wpa_s instance
3587 * is on top of a P2P group interface */
3592 * wpas_p2p_deinit_global - Deinitialize global P2P module
3593 * @global: Pointer to global data from wpa_supplicant_init()
3595 * This function deinitializes the global (per device) P2P module.
3597 void wpas_p2p_deinit_global(struct wpa_global
*global
)
3599 struct wpa_supplicant
*wpa_s
, *tmp
;
3601 wpa_s
= global
->ifaces
;
3603 wpas_p2p_service_flush(wpa_s
);
3605 if (global
->p2p
== NULL
)
3608 /* Remove remaining P2P group interfaces */
3609 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
3610 wpa_s
= wpa_s
->next
;
3612 tmp
= global
->ifaces
;
3615 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
3620 /* Disconnect from the P2P group and deinit the interface */
3621 wpas_p2p_disconnect(tmp
);
3625 * Deinit GO data on any possibly remaining interface (if main
3626 * interface is used as GO).
3628 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3629 if (wpa_s
->ap_iface
)
3630 wpas_p2p_group_deinit(wpa_s
);
3633 p2p_deinit(global
->p2p
);
3635 global
->p2p_init_wpa_s
= NULL
;
3639 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
3641 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE
) &&
3642 wpa_s
->conf
->p2p_no_group_iface
)
3643 return 0; /* separate interface disabled per configuration */
3644 if (wpa_s
->drv_flags
&
3645 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
3646 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
3647 return 1; /* P2P group requires a new interface in every case
3649 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
3650 return 0; /* driver does not support concurrent operations */
3651 if (wpa_s
->global
->ifaces
->next
)
3652 return 1; /* more that one interface already in use */
3653 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
3654 return 1; /* this interface is already in use */
3659 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
3660 const u8
*peer_addr
,
3661 enum p2p_wps_method wps_method
,
3662 int go_intent
, const u8
*own_interface_addr
,
3663 unsigned int force_freq
, int persistent_group
,
3664 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
3666 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3667 persistent_group
= 2;
3669 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3670 return wpa_drv_p2p_connect(wpa_s
, peer_addr
, wps_method
,
3671 go_intent
, own_interface_addr
,
3672 force_freq
, persistent_group
);
3676 * Increase GO config timeout if HT40 is used since it takes some time
3677 * to scan channels for coex purposes before the BSS can be started.
3679 p2p_set_config_timeout(wpa_s
->global
->p2p
,
3680 wpa_s
->p2p_go_ht40
? 255 : 100, 20);
3682 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
3683 go_intent
, own_interface_addr
, force_freq
,
3684 persistent_group
, ssid
? ssid
->ssid
: NULL
,
3685 ssid
? ssid
->ssid_len
: 0,
3686 wpa_s
->p2p_pd_before_go_neg
, pref_freq
);
3690 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
3691 const u8
*peer_addr
,
3692 enum p2p_wps_method wps_method
,
3693 int go_intent
, const u8
*own_interface_addr
,
3694 unsigned int force_freq
, int persistent_group
,
3695 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
3697 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3698 persistent_group
= 2;
3700 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3703 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
3704 go_intent
, own_interface_addr
, force_freq
,
3705 persistent_group
, ssid
? ssid
->ssid
: NULL
,
3706 ssid
? ssid
->ssid_len
: 0, pref_freq
);
3710 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
3712 wpa_s
->p2p_join_scan_count
++;
3713 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
3714 wpa_s
->p2p_join_scan_count
);
3715 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
3716 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
3717 " for join operationg - stop join attempt",
3718 MAC2STR(wpa_s
->pending_join_iface_addr
));
3719 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3720 if (wpa_s
->p2p_auto_pd
) {
3721 wpa_s
->p2p_auto_pd
= 0;
3722 wpa_msg_global(wpa_s
, MSG_INFO
,
3723 P2P_EVENT_PROV_DISC_FAILURE
3724 " p2p_dev_addr=" MACSTR
" status=N/A",
3725 MAC2STR(wpa_s
->pending_join_dev_addr
));
3728 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
3729 P2P_EVENT_GROUP_FORMATION_FAILURE
);
3734 static int wpas_check_freq_conflict(struct wpa_supplicant
*wpa_s
, int freq
)
3736 int *freqs
, res
, num
, i
;
3738 if (wpas_p2p_num_unused_channels(wpa_s
) > 0) {
3739 /* Multiple channels are supported and not all are in use */
3743 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
3747 num
= wpas_p2p_valid_oper_freqs(wpa_s
, freqs
,
3748 wpa_s
->num_multichan_concurrent
);
3754 for (i
= 0; i
< num
; i
++) {
3755 if (freqs
[i
] == freq
) {
3756 wpa_printf(MSG_DEBUG
, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3771 static int wpas_p2p_peer_go(struct wpa_supplicant
*wpa_s
,
3772 const u8
*peer_dev_addr
)
3774 struct wpa_bss
*bss
;
3777 bss
= wpa_bss_get_p2p_dev_addr(wpa_s
, peer_dev_addr
);
3780 if (bss
->last_update_idx
< wpa_s
->bss_update_idx
) {
3781 wpa_printf(MSG_DEBUG
, "P2P: Peer BSS entry not updated in the "
3786 updated
= os_time_before(&wpa_s
->p2p_auto_started
, &bss
->last_update
);
3787 wpa_printf(MSG_DEBUG
, "P2P: Current BSS entry for peer updated at "
3788 "%ld.%06ld (%supdated in last scan)",
3789 bss
->last_update
.sec
, bss
->last_update
.usec
,
3790 updated
? "": "not ");
3796 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
3797 struct wpa_scan_results
*scan_res
)
3799 struct wpa_bss
*bss
;
3801 u8 iface_addr
[ETH_ALEN
];
3803 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3805 if (wpa_s
->global
->p2p_disabled
)
3808 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for %sjoin",
3809 scan_res
? (int) scan_res
->num
: -1,
3810 wpa_s
->p2p_auto_join
? "auto_" : "");
3813 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
3815 if (wpa_s
->p2p_auto_pd
) {
3816 int join
= wpas_p2p_peer_go(wpa_s
,
3817 wpa_s
->pending_join_dev_addr
);
3819 wpa_s
->auto_pd_scan_retry
< P2P_AUTO_PD_SCAN_ATTEMPTS
) {
3820 wpa_s
->auto_pd_scan_retry
++;
3821 bss
= wpa_bss_get_bssid_latest(
3822 wpa_s
, wpa_s
->pending_join_dev_addr
);
3825 wpa_printf(MSG_DEBUG
, "P2P: Scan retry %d for "
3826 "the peer " MACSTR
" at %d MHz",
3827 wpa_s
->auto_pd_scan_retry
,
3829 pending_join_dev_addr
),
3831 wpas_p2p_join_scan_req(wpa_s
, freq
);
3839 wpa_s
->p2p_auto_pd
= 0;
3840 wpa_s
->pending_pd_use
= join
? AUTO_PD_JOIN
: AUTO_PD_GO_NEG
;
3841 wpa_printf(MSG_DEBUG
, "P2P: Auto PD with " MACSTR
" join=%d",
3842 MAC2STR(wpa_s
->pending_join_dev_addr
), join
);
3843 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
3844 wpa_s
->pending_join_dev_addr
,
3845 wpa_s
->pending_pd_config_methods
, join
,
3846 0, wpa_s
->user_initiated_pd
) < 0) {
3847 wpa_s
->p2p_auto_pd
= 0;
3848 wpa_msg_global(wpa_s
, MSG_INFO
,
3849 P2P_EVENT_PROV_DISC_FAILURE
3850 " p2p_dev_addr=" MACSTR
" status=N/A",
3851 MAC2STR(wpa_s
->pending_join_dev_addr
));
3856 if (wpa_s
->p2p_auto_join
) {
3857 int join
= wpas_p2p_peer_go(wpa_s
,
3858 wpa_s
->pending_join_dev_addr
);
3860 wpa_printf(MSG_DEBUG
, "P2P: Peer was not found to be "
3861 "running a GO -> use GO Negotiation");
3862 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
,
3863 wpa_s
->p2p_pin
, wpa_s
->p2p_wps_method
,
3864 wpa_s
->p2p_persistent_group
, 0, 0, 0,
3865 wpa_s
->p2p_go_intent
,
3866 wpa_s
->p2p_connect_freq
,
3867 wpa_s
->p2p_persistent_id
,
3868 wpa_s
->p2p_pd_before_go_neg
,
3874 wpa_printf(MSG_DEBUG
, "P2P: Peer was found running GO%s -> "
3875 "try to join the group", join
? "" :
3878 wpa_s
->p2p_fallback_to_go_neg
= 1;
3881 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
3882 wpa_s
->pending_join_iface_addr
);
3884 p2p_get_interface_addr(wpa_s
->global
->p2p
,
3885 wpa_s
->pending_join_dev_addr
,
3887 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
3889 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
3890 "address for join from " MACSTR
" to " MACSTR
3891 " based on newly discovered P2P peer entry",
3892 MAC2STR(wpa_s
->pending_join_iface_addr
),
3893 MAC2STR(iface_addr
));
3894 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
3897 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
3898 wpa_s
->pending_join_iface_addr
);
3901 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
3902 "from P2P peer table: %d MHz", freq
);
3904 bss
= wpa_bss_get_bssid_latest(wpa_s
, wpa_s
->pending_join_iface_addr
);
3907 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
3908 "from BSS table: %d MHz (SSID %s)", freq
,
3909 wpa_ssid_txt(bss
->ssid
, bss
->ssid_len
));
3914 if (wpas_check_freq_conflict(wpa_s
, freq
) > 0) {
3915 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
3916 P2P_EVENT_GROUP_FORMATION_FAILURE
3917 "reason=FREQ_CONFLICT");
3921 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
3922 "prior to joining an existing group (GO " MACSTR
3924 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
3925 wpa_s
->pending_pd_before_join
= 1;
3927 switch (wpa_s
->pending_join_wps_method
) {
3928 case WPS_PIN_DISPLAY
:
3929 method
= WPS_CONFIG_KEYPAD
;
3931 case WPS_PIN_KEYPAD
:
3932 method
= WPS_CONFIG_DISPLAY
;
3935 method
= WPS_CONFIG_PUSHBUTTON
;
3942 if ((p2p_get_provisioning_info(wpa_s
->global
->p2p
,
3943 wpa_s
->pending_join_dev_addr
) ==
3946 * We have already performed provision discovery for
3947 * joining the group. Proceed directly to join
3948 * operation without duplicated provision discovery. */
3949 wpa_printf(MSG_DEBUG
, "P2P: Provision discovery "
3950 "with " MACSTR
" already done - proceed to "
3952 MAC2STR(wpa_s
->pending_join_dev_addr
));
3953 wpa_s
->pending_pd_before_join
= 0;
3957 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
3958 wpa_s
->pending_join_dev_addr
, method
, 1,
3959 freq
, wpa_s
->user_initiated_pd
) < 0) {
3960 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
3961 "Discovery Request before joining an "
3963 wpa_s
->pending_pd_before_join
= 0;
3969 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
3970 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3971 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
3972 wpas_p2p_check_join_scan_limit(wpa_s
);
3976 /* Start join operation immediately */
3977 wpas_p2p_join_start(wpa_s
);
3981 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
)
3984 struct wpa_driver_scan_params params
;
3985 struct wpabuf
*wps_ie
, *ies
;
3987 int freqs
[2] = { 0, 0 };
3989 os_memset(¶ms
, 0, sizeof(params
));
3991 /* P2P Wildcard SSID */
3992 params
.num_ssids
= 1;
3993 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
3994 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
3996 wpa_s
->wps
->dev
.p2p
= 1;
3997 wps_ie
= wps_build_probe_req_ie(DEV_PW_DEFAULT
, &wpa_s
->wps
->dev
,
3998 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
, 0,
4000 if (wps_ie
== NULL
) {
4001 wpas_p2p_scan_res_join(wpa_s
, NULL
);
4005 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
4006 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
4008 wpabuf_free(wps_ie
);
4009 wpas_p2p_scan_res_join(wpa_s
, NULL
);
4012 wpabuf_put_buf(ies
, wps_ie
);
4013 wpabuf_free(wps_ie
);
4015 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
4017 params
.p2p_probe
= 1;
4018 params
.extra_ies
= wpabuf_head(ies
);
4019 params
.extra_ies_len
= wpabuf_len(ies
);
4022 params
.freqs
= freqs
;
4026 * Run a scan to update BSS table and start Provision Discovery once
4027 * the new scan results become available.
4029 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
4031 os_get_time(&wpa_s
->scan_trigger_time
);
4032 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
4038 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
4040 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
4041 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
4042 wpas_p2p_check_join_scan_limit(wpa_s
);
4047 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
4049 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
4050 wpas_p2p_join_scan_req(wpa_s
, 0);
4054 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
4055 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
4058 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
4059 MACSTR
" dev " MACSTR
")%s",
4060 MAC2STR(iface_addr
), MAC2STR(dev_addr
),
4061 auto_join
? " (auto_join)" : "");
4063 wpa_s
->p2p_auto_pd
= 0;
4064 wpa_s
->p2p_auto_join
= !!auto_join
;
4065 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
4066 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
4067 wpa_s
->pending_join_wps_method
= wps_method
;
4069 /* Make sure we are not running find during connection establishment */
4070 wpas_p2p_stop_find(wpa_s
);
4072 wpa_s
->p2p_join_scan_count
= 0;
4073 wpas_p2p_join_scan(wpa_s
, NULL
);
4078 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
4080 struct wpa_supplicant
*group
;
4081 struct p2p_go_neg_results res
;
4082 struct wpa_bss
*bss
;
4084 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
4087 if (group
!= wpa_s
) {
4088 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
4089 sizeof(group
->p2p_pin
));
4090 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
4093 * Need to mark the current interface for p2p_group_formation
4094 * when a separate group interface is not used. This is needed
4095 * to allow p2p_cancel stop a pending p2p_connect-join.
4096 * wpas_p2p_init_group_interface() addresses this for the case
4097 * where a separate group interface is used.
4099 wpa_s
->global
->p2p_group_formation
= wpa_s
;
4102 group
->p2p_in_provisioning
= 1;
4103 group
->p2p_fallback_to_go_neg
= wpa_s
->p2p_fallback_to_go_neg
;
4105 os_memset(&res
, 0, sizeof(res
));
4106 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
4108 res
.wps_method
= wpa_s
->pending_join_wps_method
;
4109 bss
= wpa_bss_get_bssid_latest(wpa_s
, wpa_s
->pending_join_iface_addr
);
4111 res
.freq
= bss
->freq
;
4112 res
.ssid_len
= bss
->ssid_len
;
4113 os_memcpy(res
.ssid
, bss
->ssid
, bss
->ssid_len
);
4114 wpa_printf(MSG_DEBUG
, "P2P: Join target GO operating frequency "
4115 "from BSS table: %d MHz (SSID %s)", bss
->freq
,
4116 wpa_ssid_txt(bss
->ssid
, bss
->ssid_len
));
4119 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
4120 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel prior to "
4122 wpa_drv_cancel_remain_on_channel(wpa_s
);
4123 wpa_s
->off_channel_freq
= 0;
4124 wpa_s
->roc_waiting_drv_freq
= 0;
4126 wpas_start_wps_enrollee(group
, &res
);
4129 * Allow a longer timeout for join-a-running-group than normal 15
4130 * second group formation timeout since the GO may not have authorized
4131 * our connection yet.
4133 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
4134 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
4141 static int wpas_p2p_setup_freqs(struct wpa_supplicant
*wpa_s
, int freq
,
4142 int *force_freq
, int *pref_freq
, int go
)
4145 unsigned int freq_in_use
= 0, num
, i
;
4147 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
4151 num
= get_shared_radio_freqs(wpa_s
, freqs
,
4152 wpa_s
->num_multichan_concurrent
);
4153 wpa_printf(MSG_DEBUG
,
4154 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4155 freq
, wpa_s
->num_multichan_concurrent
, num
);
4160 ret
= p2p_supported_freq(wpa_s
->global
->p2p
, freq
);
4162 ret
= p2p_supported_freq_cli(wpa_s
->global
->p2p
, freq
);
4164 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
4165 "(%u MHz) is not supported for P2P uses",
4171 for (i
= 0; i
< num
; i
++) {
4172 if (freqs
[i
] == freq
)
4176 if (num
== wpa_s
->num_multichan_concurrent
&& !freq_in_use
) {
4177 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4182 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
4183 "requested channel (%u MHz)", freq
);
4188 for (i
= 0; i
< num
; i
++) {
4189 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freqs
[i
]))
4192 if (*pref_freq
== 0 && num
< wpa_s
->num_multichan_concurrent
) {
4193 wpa_printf(MSG_DEBUG
, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4195 *pref_freq
= freqs
[i
];
4197 wpa_printf(MSG_DEBUG
, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4199 *force_freq
= freqs
[i
];
4205 if (num
< wpa_s
->num_multichan_concurrent
&& num
> 0) {
4206 wpa_printf(MSG_DEBUG
, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4208 } else if (num
< wpa_s
->num_multichan_concurrent
) {
4209 wpa_printf(MSG_DEBUG
, "P2P: No current operating channels - try to use a new channel");
4212 wpa_printf(MSG_DEBUG
, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4227 * wpas_p2p_connect - Request P2P Group Formation to be started
4228 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4229 * @peer_addr: Address of the peer P2P Device
4230 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4231 * @persistent_group: Whether to create a persistent group
4232 * @auto_join: Whether to select join vs. GO Negotiation automatically
4233 * @join: Whether to join an existing group (as a client) instead of starting
4234 * Group Owner negotiation; @peer_addr is BSSID in that case
4235 * @auth: Whether to only authorize the connection instead of doing that and
4236 * initiating Group Owner negotiation
4237 * @go_intent: GO Intent or -1 to use default
4238 * @freq: Frequency for the group or 0 for auto-selection
4239 * @persistent_id: Persistent group credentials to use for forcing GO
4240 * parameters or -1 to generate new values (SSID/passphrase)
4241 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4242 * interoperability workaround when initiating group formation
4243 * @ht40: Start GO with 40 MHz channel width
4244 * @vht: Start GO with VHT support
4245 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4246 * failure, -2 on failure due to channel not currently available,
4247 * -3 if forced channel is not supported
4249 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4250 const char *pin
, enum p2p_wps_method wps_method
,
4251 int persistent_group
, int auto_join
, int join
, int auth
,
4252 int go_intent
, int freq
, int persistent_id
, int pd
,
4255 int force_freq
= 0, pref_freq
= 0;
4257 enum wpa_driver_if_type iftype
;
4259 struct wpa_ssid
*ssid
= NULL
;
4261 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4264 if (persistent_id
>= 0) {
4265 ssid
= wpa_config_get_network(wpa_s
->conf
, persistent_id
);
4266 if (ssid
== NULL
|| ssid
->disabled
!= 2 ||
4267 ssid
->mode
!= WPAS_MODE_P2P_GO
)
4271 os_free(wpa_s
->global
->add_psk
);
4272 wpa_s
->global
->add_psk
= NULL
;
4275 go_intent
= wpa_s
->conf
->p2p_go_intent
;
4278 wpa_s
->p2p_long_listen
= 0;
4280 wpa_s
->p2p_wps_method
= wps_method
;
4281 wpa_s
->p2p_persistent_group
= !!persistent_group
;
4282 wpa_s
->p2p_persistent_id
= persistent_id
;
4283 wpa_s
->p2p_go_intent
= go_intent
;
4284 wpa_s
->p2p_connect_freq
= freq
;
4285 wpa_s
->p2p_fallback_to_go_neg
= 0;
4286 wpa_s
->p2p_pd_before_go_neg
= !!pd
;
4287 wpa_s
->p2p_go_ht40
= !!ht40
;
4288 wpa_s
->p2p_go_vht
= !!vht
;
4291 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
4292 else if (wps_method
== WPS_PIN_DISPLAY
) {
4293 ret
= wps_generate_pin();
4294 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
4296 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
4299 wpa_s
->p2p_pin
[0] = '\0';
4301 if (join
|| auto_join
) {
4302 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
4304 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
4305 "connect a running group from " MACSTR
,
4306 MAC2STR(peer_addr
));
4307 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
4310 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
4311 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
4313 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
4314 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
4318 os_get_time(&wpa_s
->p2p_auto_started
);
4319 wpa_printf(MSG_DEBUG
, "P2P: Auto join started at "
4321 wpa_s
->p2p_auto_started
.sec
,
4322 wpa_s
->p2p_auto_started
.usec
);
4324 wpa_s
->user_initiated_pd
= 1;
4325 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
,
4331 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
4335 wpas_p2p_set_own_freq_preference(wpa_s
,
4336 force_freq
? force_freq
: pref_freq
);
4338 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
4340 if (wpa_s
->create_p2p_iface
) {
4341 /* Prepare to add a new interface for the group */
4342 iftype
= WPA_IF_P2P_GROUP
;
4343 if (go_intent
== 15)
4344 iftype
= WPA_IF_P2P_GO
;
4345 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
4346 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
4347 "interface for the group");
4351 if_addr
= wpa_s
->pending_interface_addr
;
4353 if_addr
= wpa_s
->own_addr
;
4356 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
4358 force_freq
, persistent_group
, ssid
,
4364 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
4365 go_intent
, if_addr
, force_freq
,
4366 persistent_group
, ssid
, pref_freq
) < 0) {
4367 if (wpa_s
->create_p2p_iface
)
4368 wpas_p2p_remove_pending_group_interface(wpa_s
);
4376 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4377 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4378 * @freq: Frequency of the channel in MHz
4379 * @duration: Duration of the stay on the channel in milliseconds
4381 * This callback is called when the driver indicates that it has started the
4382 * requested remain-on-channel duration.
4384 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
4385 unsigned int freq
, unsigned int duration
)
4387 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4389 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
4390 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
4391 wpa_s
->pending_listen_duration
);
4392 wpa_s
->pending_listen_freq
= 0;
4397 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
4398 unsigned int timeout
)
4400 /* Limit maximum Listen state time based on driver limitation. */
4401 if (timeout
> wpa_s
->max_remain_on_chan
)
4402 timeout
= wpa_s
->max_remain_on_chan
;
4404 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4405 return wpa_drv_p2p_listen(wpa_s
, timeout
);
4407 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
4412 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4413 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4414 * @freq: Frequency of the channel in MHz
4416 * This callback is called when the driver indicates that a remain-on-channel
4417 * operation has been completed, i.e., the duration on the requested channel
4420 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
4423 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
4424 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4425 wpa_s
->p2p_long_listen
, offchannel_pending_action_tx(wpa_s
));
4426 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4428 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
4429 return; /* P2P module started a new operation */
4430 if (offchannel_pending_action_tx(wpa_s
))
4432 if (wpa_s
->p2p_long_listen
> 0)
4433 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
4434 if (wpa_s
->p2p_long_listen
> 0) {
4435 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
4436 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
4439 * When listen duration is over, stop listen & update p2p_state
4442 p2p_stop_listen(wpa_s
->global
->p2p
);
4448 * wpas_p2p_group_remove - Remove a P2P group
4449 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4450 * @ifname: Network interface name of the group interface or "*" to remove all
4452 * Returns: 0 on success, -1 on failure
4454 * This function is used to remove a P2P group. This can be used to disconnect
4455 * from a group in which the local end is a P2P Client or to end a P2P Group in
4456 * case the local end is the Group Owner. If a virtual network interface was
4457 * created for this group, that interface will be removed. Otherwise, only the
4458 * configured P2P group network will be removed from the interface.
4460 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
4462 struct wpa_global
*global
= wpa_s
->global
;
4464 if (os_strcmp(ifname
, "*") == 0) {
4465 struct wpa_supplicant
*prev
;
4466 wpa_s
= global
->ifaces
;
4469 wpa_s
= wpa_s
->next
;
4470 if (prev
->p2p_group_interface
!=
4471 NOT_P2P_GROUP_INTERFACE
||
4472 (prev
->current_ssid
&&
4473 prev
->current_ssid
->p2p_group
))
4474 wpas_p2p_disconnect(prev
);
4479 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4480 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
4484 return wpas_p2p_disconnect(wpa_s
);
4488 static int wpas_p2p_select_go_freq(struct wpa_supplicant
*wpa_s
, int freq
)
4493 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
4495 if (wpa_s
->best_24_freq
> 0 &&
4496 p2p_supported_freq_go(wpa_s
->global
->p2p
,
4497 wpa_s
->best_24_freq
)) {
4498 freq
= wpa_s
->best_24_freq
;
4499 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
4500 "channel: %d MHz", freq
);
4502 os_get_random((u8
*) &r
, sizeof(r
));
4503 freq
= 2412 + (r
% 3) * 25;
4504 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
4505 "channel: %d MHz", freq
);
4510 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
4512 if (wpa_s
->best_5_freq
> 0 &&
4513 p2p_supported_freq_go(wpa_s
->global
->p2p
,
4514 wpa_s
->best_5_freq
)) {
4515 freq
= wpa_s
->best_5_freq
;
4516 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
4517 "channel: %d MHz", freq
);
4519 os_get_random((u8
*) &r
, sizeof(r
));
4520 freq
= 5180 + (r
% 4) * 20;
4521 if (!p2p_supported_freq_go(wpa_s
->global
->p2p
, freq
)) {
4522 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
4523 "5 GHz channel for P2P group");
4526 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
4527 "channel: %d MHz", freq
);
4531 if (freq
> 0 && !p2p_supported_freq_go(wpa_s
->global
->p2p
, freq
)) {
4532 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
4533 "(%u MHz) is not supported for P2P uses",
4542 static int wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
4543 struct p2p_go_neg_results
*params
,
4544 int freq
, int ht40
, int vht
,
4545 const struct p2p_channels
*channels
)
4548 unsigned int pref_freq
;
4549 unsigned int num
, i
;
4551 os_memset(params
, 0, sizeof(*params
));
4552 params
->role_go
= 1;
4553 params
->ht40
= ht40
;
4556 if (!freq_included(channels
, freq
)) {
4557 wpa_printf(MSG_DEBUG
, "P2P: Forced GO freq %d MHz not "
4561 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
4562 "frequency %d MHz", freq
);
4563 params
->freq
= freq
;
4564 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
4565 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
4566 wpa_s
->conf
->p2p_oper_channel
<= 11 &&
4567 freq_included(channels
,
4568 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
)) {
4569 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
4570 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
4571 "frequency %d MHz", params
->freq
);
4572 } else if ((wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
4573 wpa_s
->conf
->p2p_oper_reg_class
== 116 ||
4574 wpa_s
->conf
->p2p_oper_reg_class
== 117 ||
4575 wpa_s
->conf
->p2p_oper_reg_class
== 124 ||
4576 wpa_s
->conf
->p2p_oper_reg_class
== 126 ||
4577 wpa_s
->conf
->p2p_oper_reg_class
== 127) &&
4578 freq_included(channels
,
4579 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
)) {
4580 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
4581 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
4582 "frequency %d MHz", params
->freq
);
4583 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
4584 wpa_s
->best_overall_freq
> 0 &&
4585 p2p_supported_freq_go(wpa_s
->global
->p2p
,
4586 wpa_s
->best_overall_freq
) &&
4587 freq_included(channels
, wpa_s
->best_overall_freq
)) {
4588 params
->freq
= wpa_s
->best_overall_freq
;
4589 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
4590 "channel %d MHz", params
->freq
);
4591 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
4592 wpa_s
->best_24_freq
> 0 &&
4593 p2p_supported_freq_go(wpa_s
->global
->p2p
,
4594 wpa_s
->best_24_freq
) &&
4595 freq_included(channels
, wpa_s
->best_24_freq
)) {
4596 params
->freq
= wpa_s
->best_24_freq
;
4597 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
4598 "channel %d MHz", params
->freq
);
4599 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
4600 wpa_s
->best_5_freq
> 0 &&
4601 p2p_supported_freq_go(wpa_s
->global
->p2p
,
4602 wpa_s
->best_5_freq
) &&
4603 freq_included(channels
, wpa_s
->best_5_freq
)) {
4604 params
->freq
= wpa_s
->best_5_freq
;
4605 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
4606 "channel %d MHz", params
->freq
);
4607 } else if ((pref_freq
= p2p_get_pref_freq(wpa_s
->global
->p2p
,
4609 params
->freq
= pref_freq
;
4610 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz from preferred "
4611 "channels", params
->freq
);
4614 for (chan
= 0; chan
< 11; chan
++) {
4615 params
->freq
= 2412 + chan
* 5;
4616 if (!wpas_p2p_disallowed_freq(wpa_s
->global
,
4618 freq_included(channels
, params
->freq
))
4622 wpa_printf(MSG_DEBUG
, "P2P: No 2.4 GHz channel "
4626 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
4627 "known)", params
->freq
);
4630 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
4634 res
= wpas_p2p_valid_oper_freqs(wpa_s
, freqs
,
4635 wpa_s
->num_multichan_concurrent
);
4642 for (i
= 0; i
< num
; i
++) {
4643 if (freq
&& freqs
[i
] == freq
)
4645 if (!freq
&& freq_included(channels
, freqs
[i
])) {
4646 wpa_printf(MSG_DEBUG
, "P2P: Force GO on a channel we are already using (%u MHz)",
4648 params
->freq
= freqs
[i
];
4654 if (wpas_p2p_num_unused_channels(wpa_s
) <= 0) {
4656 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq
);
4658 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on any of the channels we are already using");
4661 } else if (num
== 0) {
4662 wpa_printf(MSG_DEBUG
, "P2P: Use one of the free channels");
4664 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4673 static struct wpa_supplicant
*
4674 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
4677 struct wpa_supplicant
*group_wpa_s
;
4679 if (!wpas_p2p_create_iface(wpa_s
)) {
4680 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use same interface for group "
4682 wpa_s
->p2p_first_connection_timeout
= 0;
4686 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
4687 WPA_IF_P2P_CLIENT
) < 0) {
4688 wpa_msg_global(wpa_s
, MSG_ERROR
,
4689 "P2P: Failed to add group interface");
4692 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
4693 if (group_wpa_s
== NULL
) {
4694 wpa_msg_global(wpa_s
, MSG_ERROR
,
4695 "P2P: Failed to initialize group interface");
4696 wpas_p2p_remove_pending_group_interface(wpa_s
);
4700 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use separate group interface %s",
4701 group_wpa_s
->ifname
);
4702 group_wpa_s
->p2p_first_connection_timeout
= 0;
4708 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4709 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4710 * @persistent_group: Whether to create a persistent group
4711 * @freq: Frequency for the group or 0 to indicate no hardcoding
4712 * @ht40: Start GO with 40 MHz channel width
4713 * @vht: Start GO with VHT support
4714 * Returns: 0 on success, -1 on failure
4716 * This function creates a new P2P group with the local end as the Group Owner,
4717 * i.e., without using Group Owner Negotiation.
4719 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
4720 int freq
, int ht40
, int vht
)
4722 struct p2p_go_neg_results params
;
4724 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4727 os_free(wpa_s
->global
->add_psk
);
4728 wpa_s
->global
->add_psk
= NULL
;
4730 /* Make sure we are not running find during connection establishment */
4731 wpa_printf(MSG_DEBUG
, "P2P: Stop any on-going P2P FIND");
4732 wpas_p2p_stop_find_oper(wpa_s
);
4734 freq
= wpas_p2p_select_go_freq(wpa_s
, freq
);
4738 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
, vht
, NULL
))
4741 !p2p_supported_freq_go(wpa_s
->global
->p2p
, params
.freq
)) {
4742 wpa_printf(MSG_DEBUG
, "P2P: The selected channel for GO "
4743 "(%u MHz) is not supported for P2P uses",
4747 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
4748 params
.persistent_group
= persistent_group
;
4750 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
4753 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
4759 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
4760 struct wpa_ssid
*params
, int addr_allocated
)
4762 struct wpa_ssid
*ssid
;
4764 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
4767 wpa_s
->p2p_last_4way_hs_fail
= NULL
;
4769 wpa_supplicant_ap_deinit(wpa_s
);
4771 ssid
= wpa_config_add_network(wpa_s
->conf
);
4774 wpa_config_set_network_defaults(ssid
);
4775 ssid
->temporary
= 1;
4776 ssid
->proto
= WPA_PROTO_RSN
;
4777 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
4778 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
4779 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
4780 ssid
->ssid
= os_malloc(params
->ssid_len
);
4781 if (ssid
->ssid
== NULL
) {
4782 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
4785 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
4786 ssid
->ssid_len
= params
->ssid_len
;
4787 ssid
->p2p_group
= 1;
4788 ssid
->export_keys
= 1;
4789 if (params
->psk_set
) {
4790 os_memcpy(ssid
->psk
, params
->psk
, 32);
4793 if (params
->passphrase
)
4794 ssid
->passphrase
= os_strdup(params
->passphrase
);
4796 wpa_s
->show_group_started
= 1;
4798 wpa_supplicant_select_network(wpa_s
, ssid
);
4804 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
4805 struct wpa_ssid
*ssid
, int addr_allocated
,
4806 int freq
, int ht40
, int vht
,
4807 const struct p2p_channels
*channels
,
4808 int connection_timeout
)
4810 struct p2p_go_neg_results params
;
4813 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
4816 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
4817 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
4818 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
4823 os_free(wpa_s
->global
->add_psk
);
4824 wpa_s
->global
->add_psk
= NULL
;
4826 /* Make sure we are not running find during connection establishment */
4827 wpas_p2p_stop_find_oper(wpa_s
);
4829 wpa_s
->p2p_fallback_to_go_neg
= 0;
4831 if (ssid
->mode
== WPAS_MODE_INFRA
)
4832 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
4834 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
4837 freq
= wpas_p2p_select_go_freq(wpa_s
, freq
);
4841 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
, vht
, channels
))
4845 params
.psk_set
= ssid
->psk_set
;
4847 os_memcpy(params
.psk
, ssid
->psk
, sizeof(params
.psk
));
4848 if (ssid
->passphrase
) {
4849 if (os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
4850 wpa_printf(MSG_ERROR
, "P2P: Invalid passphrase in "
4851 "persistent group");
4854 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
4855 sizeof(params
.passphrase
));
4857 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
4858 params
.ssid_len
= ssid
->ssid_len
;
4859 params
.persistent_group
= 1;
4861 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
4865 wpa_s
->p2p_first_connection_timeout
= connection_timeout
;
4866 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
4872 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
4873 struct wpabuf
*proberesp_ies
)
4875 struct wpa_supplicant
*wpa_s
= ctx
;
4876 if (wpa_s
->ap_iface
) {
4877 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
4878 if (!(hapd
->conf
->p2p
& P2P_GROUP_OWNER
)) {
4879 wpabuf_free(beacon_ies
);
4880 wpabuf_free(proberesp_ies
);
4884 wpabuf_free(hapd
->p2p_beacon_ie
);
4885 hapd
->p2p_beacon_ie
= beacon_ies
;
4887 wpabuf_free(hapd
->p2p_probe_resp_ie
);
4888 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
4890 wpabuf_free(beacon_ies
);
4891 wpabuf_free(proberesp_ies
);
4893 wpa_supplicant_ap_update_beacon(wpa_s
);
4897 static void wpas_p2p_idle_update(void *ctx
, int idle
)
4899 struct wpa_supplicant
*wpa_s
= ctx
;
4900 if (!wpa_s
->ap_iface
)
4902 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
4904 wpas_p2p_set_group_idle_timeout(wpa_s
);
4906 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
4910 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
4911 struct wpa_ssid
*ssid
)
4913 struct p2p_group
*group
;
4914 struct p2p_group_config
*cfg
;
4916 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4918 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4921 cfg
= os_zalloc(sizeof(*cfg
));
4925 if (ssid
->p2p_persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
4926 cfg
->persistent_group
= 2;
4927 else if (ssid
->p2p_persistent_group
)
4928 cfg
->persistent_group
= 1;
4929 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
4930 if (wpa_s
->max_stations
&&
4931 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
4932 cfg
->max_clients
= wpa_s
->max_stations
;
4934 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
4935 os_memcpy(cfg
->ssid
, ssid
->ssid
, ssid
->ssid_len
);
4936 cfg
->ssid_len
= ssid
->ssid_len
;
4937 cfg
->cb_ctx
= wpa_s
;
4938 cfg
->ie_update
= wpas_p2p_ie_update
;
4939 cfg
->idle_update
= wpas_p2p_idle_update
;
4941 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
4944 if (ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
)
4945 p2p_group_notif_formation_done(group
);
4946 wpa_s
->p2p_group
= group
;
4951 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4954 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
4956 if (!wpa_s
->p2p_in_provisioning
) {
4957 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
4958 "provisioning not in progress");
4962 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
4963 u8 go_dev_addr
[ETH_ALEN
];
4964 os_memcpy(go_dev_addr
, wpa_s
->bssid
, ETH_ALEN
);
4965 wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
4967 /* Clear any stored provisioning info */
4968 p2p_clear_provisioning_info(wpa_s
->global
->p2p
, go_dev_addr
);
4971 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
4973 wpa_s
->p2p_go_group_formation_completed
= 1;
4974 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
4976 * Use a separate timeout for initial data connection to
4977 * complete to allow the group to be removed automatically if
4978 * something goes wrong in this step before the P2P group idle
4979 * timeout mechanism is taken into use.
4981 wpa_dbg(wpa_s
, MSG_DEBUG
,
4982 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4983 P2P_MAX_INITIAL_CONN_WAIT
);
4984 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT
, 0,
4985 wpas_p2p_group_formation_timeout
,
4986 wpa_s
->parent
, NULL
);
4989 * Use a separate timeout for initial data connection to
4990 * complete to allow the group to be removed automatically if
4991 * the client does not complete data connection successfully.
4993 wpa_dbg(wpa_s
, MSG_DEBUG
,
4994 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
4995 P2P_MAX_INITIAL_CONN_WAIT_GO
);
4996 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO
, 0,
4997 wpas_p2p_group_formation_timeout
,
4998 wpa_s
->parent
, NULL
);
5000 * Complete group formation on first successful data connection
5002 wpa_s
->p2p_go_group_formation_completed
= 0;
5004 if (wpa_s
->global
->p2p
)
5005 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
5006 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5007 wpa_drv_wps_success_cb(wpa_s
, peer_addr
);
5008 wpas_group_formation_completed(wpa_s
, 1);
5012 void wpas_p2p_wps_failed(struct wpa_supplicant
*wpa_s
,
5013 struct wps_event_fail
*fail
)
5015 if (!wpa_s
->p2p_in_provisioning
) {
5016 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS fail event - P2P "
5017 "provisioning not in progress");
5021 if (wpa_s
->go_params
) {
5022 p2p_clear_provisioning_info(
5024 wpa_s
->go_params
->peer_device_addr
);
5027 wpas_notify_p2p_wps_failed(wpa_s
, fail
);
5031 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
5032 const char *config_method
,
5033 enum wpas_p2p_prov_disc_use use
)
5037 wpa_s
->p2p_fallback_to_go_neg
= 0;
5038 wpa_s
->pending_pd_use
= NORMAL_PD
;
5039 if (os_strncmp(config_method
, "display", 7) == 0)
5040 config_methods
= WPS_CONFIG_DISPLAY
;
5041 else if (os_strncmp(config_method
, "keypad", 6) == 0)
5042 config_methods
= WPS_CONFIG_KEYPAD
;
5043 else if (os_strncmp(config_method
, "pbc", 3) == 0 ||
5044 os_strncmp(config_method
, "pushbutton", 10) == 0)
5045 config_methods
= WPS_CONFIG_PUSHBUTTON
;
5047 wpa_printf(MSG_DEBUG
, "P2P: Unknown config method");
5051 if (use
== WPAS_P2P_PD_AUTO
) {
5052 os_memcpy(wpa_s
->pending_join_dev_addr
, peer_addr
, ETH_ALEN
);
5053 wpa_s
->pending_pd_config_methods
= config_methods
;
5054 wpa_s
->p2p_auto_pd
= 1;
5055 wpa_s
->p2p_auto_join
= 0;
5056 wpa_s
->pending_pd_before_join
= 0;
5057 wpa_s
->auto_pd_scan_retry
= 0;
5058 wpas_p2p_stop_find(wpa_s
);
5059 wpa_s
->p2p_join_scan_count
= 0;
5060 os_get_time(&wpa_s
->p2p_auto_started
);
5061 wpa_printf(MSG_DEBUG
, "P2P: Auto PD started at %ld.%06ld",
5062 wpa_s
->p2p_auto_started
.sec
,
5063 wpa_s
->p2p_auto_started
.usec
);
5064 wpas_p2p_join_scan(wpa_s
, NULL
);
5068 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
5069 return wpa_drv_p2p_prov_disc_req(wpa_s
, peer_addr
,
5071 use
== WPAS_P2P_PD_FOR_JOIN
);
5074 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
5077 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
5078 config_methods
, use
== WPAS_P2P_PD_FOR_JOIN
,
5083 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
5086 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
5090 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
5092 if (!offchannel_pending_action_tx(wpa_s
))
5095 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
5096 "operation request");
5097 offchannel_clear_pending_action_tx(wpa_s
);
5101 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
5102 enum p2p_discovery_type type
,
5103 unsigned int num_req_dev_types
, const u8
*req_dev_types
,
5104 const u8
*dev_id
, unsigned int search_delay
)
5106 wpas_p2p_clear_pending_action_tx(wpa_s
);
5107 wpa_s
->p2p_long_listen
= 0;
5109 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5110 return wpa_drv_p2p_find(wpa_s
, timeout
, type
);
5112 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
||
5113 wpa_s
->p2p_in_provisioning
)
5116 wpa_supplicant_cancel_sched_scan(wpa_s
);
5118 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
,
5119 num_req_dev_types
, req_dev_types
, dev_id
,
5124 static int wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
)
5126 wpas_p2p_clear_pending_action_tx(wpa_s
);
5127 wpa_s
->p2p_long_listen
= 0;
5128 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
5129 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
5130 wpa_s
->global
->p2p_cb_on_scan_complete
= 0;
5132 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
5133 wpa_drv_p2p_stop_find(wpa_s
);
5137 if (wpa_s
->global
->p2p
)
5138 p2p_stop_find(wpa_s
->global
->p2p
);
5144 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
5146 if (wpas_p2p_stop_find_oper(wpa_s
) > 0)
5148 wpas_p2p_remove_pending_group_interface(wpa_s
);
5152 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
5154 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
5155 wpa_s
->p2p_long_listen
= 0;
5159 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
5163 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5166 wpa_supplicant_cancel_sched_scan(wpa_s
);
5167 wpas_p2p_clear_pending_action_tx(wpa_s
);
5171 * This is a request for unlimited Listen state. However, at
5172 * least for now, this is mapped to a Listen state for one
5177 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
5178 wpa_s
->p2p_long_listen
= 0;
5181 * Stop previous find/listen operation to avoid trying to request a new
5182 * remain-on-channel operation while the driver is still running the
5185 if (wpa_s
->global
->p2p
)
5186 p2p_stop_find(wpa_s
->global
->p2p
);
5188 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
5189 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
5190 wpa_s
->p2p_long_listen
= timeout
* 1000;
5191 eloop_register_timeout(timeout
, 0,
5192 wpas_p2p_long_listen_timeout
,
5200 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
5201 u8
*buf
, size_t len
, int p2p_group
)
5203 struct wpabuf
*p2p_ie
;
5206 if (wpa_s
->global
->p2p_disabled
)
5208 if (wpa_s
->global
->p2p
== NULL
)
5213 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
5214 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
5216 wpabuf_free(p2p_ie
);
5222 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
5223 const u8
*dst
, const u8
*bssid
,
5224 const u8
*ie
, size_t ie_len
, int ssi_signal
)
5226 if (wpa_s
->global
->p2p_disabled
)
5228 if (wpa_s
->global
->p2p
== NULL
)
5231 switch (p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, dst
, bssid
,
5233 case P2P_PREQ_NOT_P2P
:
5234 wpas_notify_preq(wpa_s
, addr
, dst
, bssid
, ie
, ie_len
,
5237 case P2P_PREQ_MALFORMED
:
5238 case P2P_PREQ_NOT_LISTEN
:
5239 case P2P_PREQ_NOT_PROCESSED
:
5240 default: /* make gcc happy */
5242 case P2P_PREQ_PROCESSED
:
5248 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
5249 const u8
*sa
, const u8
*bssid
,
5250 u8 category
, const u8
*data
, size_t len
, int freq
)
5252 if (wpa_s
->global
->p2p_disabled
)
5254 if (wpa_s
->global
->p2p
== NULL
)
5257 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
5262 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
5264 if (wpa_s
->global
->p2p_disabled
)
5266 if (wpa_s
->global
->p2p
== NULL
)
5269 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
5273 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
5275 p2p_group_deinit(wpa_s
->p2p_group
);
5276 wpa_s
->p2p_group
= NULL
;
5278 wpa_s
->ap_configured_cb
= NULL
;
5279 wpa_s
->ap_configured_cb_ctx
= NULL
;
5280 wpa_s
->ap_configured_cb_data
= NULL
;
5281 wpa_s
->connect_without_scan
= NULL
;
5285 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
5287 wpa_s
->p2p_long_listen
= 0;
5289 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5290 return wpa_drv_p2p_reject(wpa_s
, addr
);
5292 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5295 return p2p_reject(wpa_s
->global
->p2p
, addr
);
5299 /* Invite to reinvoke a persistent group */
5300 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
5301 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
, int freq
,
5302 int ht40
, int vht
, int pref_freq
)
5304 enum p2p_invite_role role
;
5308 int no_pref_freq_given
= pref_freq
== 0;
5310 wpa_s
->global
->p2p_invite_group
= NULL
;
5312 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
5314 os_memset(wpa_s
->p2p_auth_invite
, 0, ETH_ALEN
);
5316 wpa_s
->p2p_persistent_go_freq
= freq
;
5317 wpa_s
->p2p_go_ht40
= !!ht40
;
5318 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
5319 role
= P2P_INVITE_ROLE_GO
;
5320 if (peer_addr
== NULL
) {
5321 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
5322 "address in invitation command");
5325 if (wpas_p2p_create_iface(wpa_s
)) {
5326 if (wpas_p2p_add_group_interface(wpa_s
,
5327 WPA_IF_P2P_GO
) < 0) {
5328 wpa_printf(MSG_ERROR
, "P2P: Failed to "
5329 "allocate a new interface for the "
5333 bssid
= wpa_s
->pending_interface_addr
;
5335 bssid
= wpa_s
->own_addr
;
5337 role
= P2P_INVITE_ROLE_CLIENT
;
5338 peer_addr
= ssid
->bssid
;
5340 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
5342 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
5343 role
== P2P_INVITE_ROLE_GO
);
5347 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5348 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
5349 ssid
->ssid
, ssid
->ssid_len
,
5352 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5355 if (wpa_s
->parent
->conf
->p2p_ignore_shared_freq
&&
5356 no_pref_freq_given
&& pref_freq
> 0 &&
5357 wpa_s
->num_multichan_concurrent
> 1 &&
5358 wpas_p2p_num_unused_channels(wpa_s
) > 0) {
5359 wpa_printf(MSG_DEBUG
, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5364 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
5365 ssid
->ssid
, ssid
->ssid_len
, force_freq
, go_dev_addr
,
5370 /* Invite to join an active group */
5371 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
5372 const u8
*peer_addr
, const u8
*go_dev_addr
)
5374 struct wpa_global
*global
= wpa_s
->global
;
5375 enum p2p_invite_role role
;
5377 struct wpa_ssid
*ssid
;
5379 int freq
= 0, force_freq
= 0, pref_freq
= 0;
5382 wpa_s
->p2p_persistent_go_freq
= 0;
5383 wpa_s
->p2p_go_ht40
= 0;
5384 wpa_s
->p2p_go_vht
= 0;
5386 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
5387 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
5390 if (wpa_s
== NULL
) {
5391 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
5395 ssid
= wpa_s
->current_ssid
;
5397 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
5402 wpa_s
->global
->p2p_invite_group
= wpa_s
;
5403 persistent
= ssid
->p2p_persistent_group
&&
5404 wpas_p2p_get_persistent(wpa_s
->parent
, peer_addr
,
5405 ssid
->ssid
, ssid
->ssid_len
);
5407 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
5408 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
5409 bssid
= wpa_s
->own_addr
;
5410 if (go_dev_addr
== NULL
)
5411 go_dev_addr
= wpa_s
->global
->p2p_dev_addr
;
5412 freq
= ssid
->frequency
;
5414 role
= P2P_INVITE_ROLE_CLIENT
;
5415 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
5416 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
5417 "invite to current group");
5420 bssid
= wpa_s
->bssid
;
5421 if (go_dev_addr
== NULL
&&
5422 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
5423 go_dev_addr
= wpa_s
->go_dev_addr
;
5424 freq
= wpa_s
->current_bss
? wpa_s
->current_bss
->freq
:
5425 (int) wpa_s
->assoc_freq
;
5427 wpa_s
->parent
->pending_invite_ssid_id
= -1;
5429 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5430 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
5431 ssid
->ssid
, ssid
->ssid_len
,
5432 go_dev_addr
, persistent
);
5434 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5437 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
5438 role
== P2P_INVITE_ROLE_ACTIVE_GO
);
5441 wpas_p2p_set_own_freq_preference(wpa_s
, force_freq
);
5443 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
5444 ssid
->ssid
, ssid
->ssid_len
, force_freq
,
5445 go_dev_addr
, persistent
, pref_freq
);
5449 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
5451 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
5452 const char *ssid_txt
;
5453 u8 go_dev_addr
[ETH_ALEN
];
5454 int network_id
= -1;
5458 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
) {
5459 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5460 wpa_s
->parent
, NULL
);
5463 if (!wpa_s
->show_group_started
|| !ssid
)
5466 wpa_s
->show_group_started
= 0;
5468 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
5469 os_memset(go_dev_addr
, 0, ETH_ALEN
);
5470 if (ssid
->bssid_set
)
5471 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
5472 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
5474 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
5476 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
5477 wpa_s
->global
->p2p_group_formation
= NULL
;
5479 freq
= wpa_s
->current_bss
? wpa_s
->current_bss
->freq
:
5480 (int) wpa_s
->assoc_freq
;
5481 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
5483 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
5484 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
5485 "%s client ssid=\"%s\" freq=%d psk=%s "
5486 "go_dev_addr=" MACSTR
"%s",
5487 wpa_s
->ifname
, ssid_txt
, freq
, psk
,
5488 MAC2STR(go_dev_addr
),
5489 persistent
? " [PERSISTENT]" : "");
5491 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
5492 "%s client ssid=\"%s\" freq=%d "
5493 "passphrase=\"%s\" go_dev_addr=" MACSTR
"%s",
5494 wpa_s
->ifname
, ssid_txt
, freq
,
5495 ssid
->passphrase
? ssid
->passphrase
: "",
5496 MAC2STR(go_dev_addr
),
5497 persistent
? " [PERSISTENT]" : "");
5501 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
5504 network_id
= ssid
->id
;
5505 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 1);
5508 wpas_p2p_continue_after_scan(wpa_s
);
5512 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
5513 u32 interval1
, u32 duration2
, u32 interval2
)
5515 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5517 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5520 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
5521 wpa_s
->current_ssid
== NULL
||
5522 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
5525 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
5526 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
5527 duration1
, interval1
, duration2
, interval2
);
5531 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
5532 unsigned int interval
)
5534 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5537 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5540 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
5544 static int wpas_p2p_is_client(struct wpa_supplicant
*wpa_s
)
5546 if (wpa_s
->current_ssid
== NULL
) {
5548 * current_ssid can be cleared when P2P client interface gets
5549 * disconnected, so assume this interface was used as P2P
5554 return wpa_s
->current_ssid
->p2p_group
&&
5555 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
;
5559 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
5561 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
5563 if (wpa_s
->conf
->p2p_group_idle
== 0 && !wpas_p2p_is_client(wpa_s
)) {
5564 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
5569 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
5571 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_IDLE_TIMEOUT
);
5575 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
5579 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
5580 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
5582 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
5585 timeout
= wpa_s
->conf
->p2p_group_idle
;
5586 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
&&
5587 (timeout
== 0 || timeout
> P2P_MAX_CLIENT_IDLE
))
5588 timeout
= P2P_MAX_CLIENT_IDLE
;
5594 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
)
5595 timeout
= 0; /* special client mode no-timeout */
5600 if (wpa_s
->p2p_in_provisioning
) {
5602 * Use the normal group formation timeout during the
5603 * provisioning phase to avoid terminating this process too
5604 * early due to group idle timeout.
5606 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
5607 "during provisioning");
5611 if (wpa_s
->show_group_started
) {
5613 * Use the normal group formation timeout between the end of
5614 * the provisioning phase and completion of 4-way handshake to
5615 * avoid terminating this process too early due to group idle
5618 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
5619 "while waiting for initial 4-way handshake to "
5624 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
5626 eloop_register_timeout(timeout
, 0, wpas_p2p_group_idle_timeout
,
5631 /* Returns 1 if the interface was removed */
5632 int wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
5633 u16 reason_code
, const u8
*ie
, size_t ie_len
,
5634 int locally_generated
)
5636 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5638 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5641 if (!locally_generated
)
5642 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
5645 if (reason_code
== WLAN_REASON_DEAUTH_LEAVING
&& !locally_generated
&&
5646 wpa_s
->current_ssid
&&
5647 wpa_s
->current_ssid
->p2p_group
&&
5648 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
) {
5649 wpa_printf(MSG_DEBUG
, "P2P: GO indicated that the P2P Group "
5650 "session is ending");
5651 if (wpas_p2p_group_delete(wpa_s
,
5652 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
)
5661 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
5662 u16 reason_code
, const u8
*ie
, size_t ie_len
,
5663 int locally_generated
)
5665 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5667 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5670 if (!locally_generated
)
5671 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
5676 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
5678 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
5683 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
5686 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
5687 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
5689 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
)
5690 p2p_set_pri_dev_type(p2p
, wpa_s
->conf
->device_type
);
5693 (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_CONFIG_METHODS
))
5694 p2p_set_config_methods(p2p
, wpa_s
->wps
->config_methods
);
5696 if (wpa_s
->wps
&& (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_UUID
))
5697 p2p_set_uuid(p2p
, wpa_s
->wps
->uuid
);
5699 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_WPS_STRING
) {
5700 p2p_set_manufacturer(p2p
, wpa_s
->conf
->manufacturer
);
5701 p2p_set_model_name(p2p
, wpa_s
->conf
->model_name
);
5702 p2p_set_model_number(p2p
, wpa_s
->conf
->model_number
);
5703 p2p_set_serial_number(p2p
, wpa_s
->conf
->serial_number
);
5706 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
)
5707 p2p_set_sec_dev_types(p2p
,
5708 (void *) wpa_s
->conf
->sec_device_type
,
5709 wpa_s
->conf
->num_sec_device_types
);
5711 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_VENDOR_EXTENSION
) {
5713 p2p_remove_wps_vendor_extensions(p2p
);
5714 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
5715 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
5717 p2p_add_wps_vendor_extension(
5718 p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
5722 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
5723 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
5725 country
[0] = wpa_s
->conf
->country
[0];
5726 country
[1] = wpa_s
->conf
->country
[1];
5728 p2p_set_country(p2p
, country
);
5731 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
5732 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
5733 wpa_s
->conf
->p2p_ssid_postfix
?
5734 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
5738 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
5739 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
5741 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_LISTEN_CHANNEL
) {
5742 u8 reg_class
, channel
;
5745 if (wpa_s
->conf
->p2p_listen_reg_class
&&
5746 wpa_s
->conf
->p2p_listen_channel
) {
5747 reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
5748 channel
= wpa_s
->conf
->p2p_listen_channel
;
5752 * Pick one of the social channels randomly as the
5755 os_get_random((u8
*) &r
, sizeof(r
));
5756 channel
= 1 + (r
% 3) * 5;
5758 ret
= p2p_set_listen_channel(p2p
, reg_class
, channel
);
5760 wpa_printf(MSG_ERROR
, "P2P: Own listen channel update "
5763 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_OPER_CHANNEL
) {
5764 u8 op_reg_class
, op_channel
, cfg_op_channel
;
5767 if (wpa_s
->conf
->p2p_oper_reg_class
&&
5768 wpa_s
->conf
->p2p_oper_channel
) {
5769 op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
5770 op_channel
= wpa_s
->conf
->p2p_oper_channel
;
5775 * Use random operation channel from (1, 6, 11)
5776 *if no other preference is indicated.
5778 os_get_random((u8
*) &r
, sizeof(r
));
5779 op_channel
= 1 + (r
% 3) * 5;
5782 ret
= p2p_set_oper_channel(p2p
, op_reg_class
, op_channel
,
5785 wpa_printf(MSG_ERROR
, "P2P: Own oper channel update "
5789 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_PREF_CHAN
) {
5790 if (p2p_set_pref_chan(p2p
, wpa_s
->conf
->num_p2p_pref_chan
,
5791 wpa_s
->conf
->p2p_pref_chan
) < 0) {
5792 wpa_printf(MSG_ERROR
, "P2P: Preferred channel list "
5796 if (p2p_set_no_go_freq(p2p
, &wpa_s
->conf
->p2p_no_go_freq
) < 0) {
5797 wpa_printf(MSG_ERROR
, "P2P: No GO channel list "
5804 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
5807 if (!wpa_s
->ap_iface
)
5809 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
5814 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
5816 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5818 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5821 wpa_s
->global
->cross_connection
= enabled
;
5822 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
5825 struct wpa_supplicant
*iface
;
5827 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
5829 if (iface
->cross_connect_enabled
== 0)
5832 iface
->cross_connect_enabled
= 0;
5833 iface
->cross_connect_in_use
= 0;
5834 wpa_msg_global(iface
->parent
, MSG_INFO
,
5835 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
5837 iface
->cross_connect_uplink
);
5845 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
5847 struct wpa_supplicant
*iface
;
5849 if (!uplink
->global
->cross_connection
)
5852 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
5853 if (!iface
->cross_connect_enabled
)
5855 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
5858 if (iface
->ap_iface
== NULL
)
5860 if (iface
->cross_connect_in_use
)
5863 iface
->cross_connect_in_use
= 1;
5864 wpa_msg_global(iface
->parent
, MSG_INFO
,
5865 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
5866 iface
->ifname
, iface
->cross_connect_uplink
);
5871 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
5873 struct wpa_supplicant
*iface
;
5875 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
5876 if (!iface
->cross_connect_enabled
)
5878 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
5881 if (!iface
->cross_connect_in_use
)
5884 wpa_msg_global(iface
->parent
, MSG_INFO
,
5885 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
5886 iface
->ifname
, iface
->cross_connect_uplink
);
5887 iface
->cross_connect_in_use
= 0;
5892 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
5894 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
5895 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
5896 wpa_s
->cross_connect_disallowed
)
5897 wpas_p2p_disable_cross_connect(wpa_s
);
5899 wpas_p2p_enable_cross_connect(wpa_s
);
5900 if (!wpa_s
->ap_iface
&&
5901 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
5902 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
5906 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
5908 wpas_p2p_disable_cross_connect(wpa_s
);
5909 if (!wpa_s
->ap_iface
&&
5910 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout
,
5912 wpas_p2p_set_group_idle_timeout(wpa_s
);
5916 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
5918 struct wpa_supplicant
*iface
;
5920 if (!wpa_s
->global
->cross_connection
)
5923 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
5926 if (iface
->drv_flags
&
5927 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
5929 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
5932 wpa_s
->cross_connect_enabled
= 1;
5933 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
5934 sizeof(wpa_s
->cross_connect_uplink
));
5935 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
5936 "%s to %s whenever uplink is available",
5937 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
5939 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
5940 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
5941 iface
->cross_connect_disallowed
||
5942 iface
->wpa_state
!= WPA_COMPLETED
)
5945 wpa_s
->cross_connect_in_use
= 1;
5946 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
5947 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
5948 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
5954 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
5956 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
5957 !wpa_s
->p2p_in_provisioning
)
5958 return 0; /* not P2P client operation */
5960 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
5962 if (wpa_s
!= wpa_s
->parent
)
5963 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
5964 wpas_p2p_group_formation_failed(wpa_s
);
5969 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
5971 struct p2p_channels chan
, cli_chan
;
5973 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
5976 os_memset(&chan
, 0, sizeof(chan
));
5977 os_memset(&cli_chan
, 0, sizeof(cli_chan
));
5978 if (wpas_p2p_setup_channels(wpa_s
, &chan
, &cli_chan
)) {
5979 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
5984 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
, &cli_chan
);
5988 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant
*wpa_s
,
5989 struct wpa_scan_results
*scan_res
)
5991 wpa_printf(MSG_DEBUG
, "P2P: Ignore scan results");
5995 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
5997 struct wpa_global
*global
= wpa_s
->global
;
6001 if (global
->p2p
== NULL
)
6004 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
6006 if (wpa_s
->pending_interface_name
[0] &&
6007 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
6010 peer
= p2p_get_go_neg_peer(global
->p2p
);
6012 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
6013 MACSTR
, MAC2STR(peer
));
6014 p2p_unauthorize(global
->p2p
, peer
);
6018 if (wpa_s
->scan_res_handler
== wpas_p2p_scan_res_join
) {
6019 wpa_printf(MSG_DEBUG
, "P2P: Stop pending scan for join");
6020 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_ignore
;
6024 if (wpa_s
->pending_pd_before_join
) {
6025 wpa_printf(MSG_DEBUG
, "P2P: Stop pending PD before join");
6026 wpa_s
->pending_pd_before_join
= 0;
6030 wpas_p2p_stop_find(wpa_s
);
6032 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
6033 if (wpa_s
== global
->p2p_group_formation
&&
6034 (wpa_s
->p2p_in_provisioning
||
6035 wpa_s
->parent
->pending_interface_type
==
6036 WPA_IF_P2P_CLIENT
)) {
6037 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
6038 "formation found - cancelling",
6041 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
6042 wpa_s
->parent
, NULL
);
6043 if (wpa_s
->p2p_in_provisioning
) {
6044 wpas_group_formation_completed(wpa_s
, 0);
6047 wpas_p2p_group_delete(wpa_s
,
6048 P2P_GROUP_REMOVAL_REQUESTED
);
6054 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
6062 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
6064 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
6067 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
6068 "being available anymore");
6069 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_UNAVAILABLE
);
6073 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
6074 int freq_24
, int freq_5
, int freq_overall
)
6076 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
6077 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
6079 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
6083 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
6086 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
6088 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
6091 if (hwaddr_aton(addr
, peer
))
6094 return p2p_unauthorize(p2p
, peer
);
6099 * wpas_p2p_disconnect - Disconnect from a P2P Group
6100 * @wpa_s: Pointer to wpa_supplicant data
6101 * Returns: 0 on success, -1 on failure
6103 * This can be used to disconnect from a group in which the local end is a P2P
6104 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6105 * virtual network interface was created for this group, that interface will be
6106 * removed. Otherwise, only the configured P2P group network will be removed
6107 * from the interface.
6109 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
6115 return wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_REQUESTED
) < 0 ?
6120 int wpas_p2p_in_progress(struct wpa_supplicant
*wpa_s
)
6124 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
6127 ret
= p2p_in_progress(wpa_s
->global
->p2p
);
6130 * Check whether there is an ongoing WPS provisioning step (or
6131 * other parts of group formation) on another interface since
6132 * p2p_in_progress() does not report this to avoid issues for
6133 * scans during such provisioning step.
6135 if (wpa_s
->global
->p2p_group_formation
&&
6136 wpa_s
->global
->p2p_group_formation
!= wpa_s
) {
6137 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Another interface (%s) "
6138 "in group formation",
6139 wpa_s
->global
->p2p_group_formation
->ifname
);
6144 if (!ret
&& wpa_s
->global
->p2p_go_wait_client
.sec
) {
6147 if (now
.sec
> wpa_s
->global
->p2p_go_wait_client
.sec
+
6148 P2P_MAX_INITIAL_CONN_WAIT_GO
) {
6149 /* Wait for the first client has expired */
6150 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
6152 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Waiting for initial client connection during group formation");
6161 void wpas_p2p_network_removed(struct wpa_supplicant
*wpa_s
,
6162 struct wpa_ssid
*ssid
)
6164 if (wpa_s
->p2p_in_provisioning
&& ssid
->p2p_group
&&
6165 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
6166 wpa_s
->parent
, NULL
) > 0) {
6168 * Remove the network by scheduling the group formation
6169 * timeout to happen immediately. The teardown code
6170 * needs to be scheduled to run asynch later so that we
6171 * don't delete data from under ourselves unexpectedly.
6172 * Calling wpas_p2p_group_formation_timeout directly
6173 * causes a series of crashes in WPS failure scenarios.
6175 wpa_printf(MSG_DEBUG
, "P2P: Canceled group formation due to "
6176 "P2P group network getting removed");
6177 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
6178 wpa_s
->parent
, NULL
);
6183 struct wpa_ssid
* wpas_p2p_get_persistent(struct wpa_supplicant
*wpa_s
,
6184 const u8
*addr
, const u8
*ssid
,
6190 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
6191 if (s
->disabled
!= 2)
6194 (ssid_len
!= s
->ssid_len
||
6195 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0))
6198 if (s
->mode
== WPAS_MODE_P2P_GO
)
6202 if (os_memcmp(s
->bssid
, addr
, ETH_ALEN
) == 0)
6203 return s
; /* peer is GO in the persistent group */
6204 if (s
->mode
!= WPAS_MODE_P2P_GO
|| s
->p2p_client_list
== NULL
)
6206 for (i
= 0; i
< s
->num_p2p_clients
; i
++) {
6207 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
,
6208 addr
, ETH_ALEN
) == 0)
6209 return s
; /* peer is P2P client in persistent
6218 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant
*wpa_s
,
6221 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
6222 wpa_s
->parent
, NULL
) > 0) {
6224 * This can happen if WPS provisioning step is not terminated
6225 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6226 * peer was able to connect, there is no need to time out group
6227 * formation after this, though. In addition, this is used with
6228 * the initial connection wait on the GO as a separate formation
6229 * timeout and as such, expected to be hit after the initial WPS
6230 * provisioning step.
6232 wpa_printf(MSG_DEBUG
, "P2P: Canceled P2P group formation timeout on data connection");
6234 if (!wpa_s
->p2p_go_group_formation_completed
) {
6235 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Marking group formation completed on GO on first data connection");
6236 wpa_s
->p2p_go_group_formation_completed
= 1;
6237 wpa_s
->global
->p2p_group_formation
= NULL
;
6238 wpa_s
->p2p_in_provisioning
= 0;
6240 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
6243 wpas_p2p_add_persistent_group_client(wpa_s
, addr
);
6247 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
6250 struct wpa_supplicant
*group
= wpa_s
;
6251 if (wpa_s
->global
->p2p_group_formation
)
6252 group
= wpa_s
->global
->p2p_group_formation
;
6253 wpa_s
= wpa_s
->parent
;
6254 offchannel_send_action_done(wpa_s
);
6256 wpas_p2p_group_delete(group
, P2P_GROUP_REMOVAL_SILENT
);
6257 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Fall back to GO Negotiation");
6258 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
, wpa_s
->p2p_pin
,
6259 wpa_s
->p2p_wps_method
, wpa_s
->p2p_persistent_group
, 0,
6260 0, 0, wpa_s
->p2p_go_intent
, wpa_s
->p2p_connect_freq
,
6261 wpa_s
->p2p_persistent_id
,
6262 wpa_s
->p2p_pd_before_go_neg
,
6268 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant
*wpa_s
)
6270 if (!wpa_s
->p2p_fallback_to_go_neg
||
6271 wpa_s
->p2p_in_provisioning
<= 5)
6274 if (wpas_p2p_peer_go(wpa_s
, wpa_s
->pending_join_dev_addr
) > 0)
6275 return 0; /* peer operating as a GO */
6277 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: GO not found for p2p_connect-auto - "
6278 "fallback to GO Negotiation");
6279 wpas_p2p_fallback_to_go_neg(wpa_s
, 1);
6285 unsigned int wpas_p2p_search_delay(struct wpa_supplicant
*wpa_s
)
6287 struct wpa_supplicant
*ifs
;
6289 if (wpa_s
->wpa_state
> WPA_SCANNING
) {
6290 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search delay due to "
6291 "concurrent operation",
6292 P2P_CONCURRENT_SEARCH_DELAY
);
6293 return P2P_CONCURRENT_SEARCH_DELAY
;
6296 dl_list_for_each(ifs
, &wpa_s
->radio
->ifaces
, struct wpa_supplicant
,
6298 if (ifs
!= wpa_s
&& ifs
->wpa_state
> WPA_SCANNING
) {
6299 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search "
6300 "delay due to concurrent operation on "
6302 P2P_CONCURRENT_SEARCH_DELAY
, ifs
->ifname
);
6303 return P2P_CONCURRENT_SEARCH_DELAY
;
6311 void wpas_p2p_continue_after_scan(struct wpa_supplicant
*wpa_s
)
6313 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Station mode scan operation not "
6314 "pending anymore (sta_scan_pending=%d "
6315 "p2p_cb_on_scan_complete=%d)", wpa_s
->sta_scan_pending
,
6316 wpa_s
->global
->p2p_cb_on_scan_complete
);
6317 wpa_s
->sta_scan_pending
= 0;
6319 if (!wpa_s
->global
->p2p_cb_on_scan_complete
)
6321 wpa_s
->global
->p2p_cb_on_scan_complete
= 0;
6323 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
6326 if (p2p_other_scan_completed(wpa_s
->global
->p2p
) == 1) {
6327 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Pending P2P operation "
6328 "continued after successful connection");
6329 p2p_increase_search_delay(wpa_s
->global
->p2p
,
6330 wpas_p2p_search_delay(wpa_s
));
6335 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant
*wpa_s
,
6336 struct wpa_ssid
*s
, const u8
*addr
,
6339 struct psk_list_entry
*psk
, *tmp
;
6342 dl_list_for_each_safe(psk
, tmp
, &s
->psk_list
, struct psk_list_entry
,
6344 if ((iface_addr
&& !psk
->p2p
&&
6345 os_memcmp(addr
, psk
->addr
, ETH_ALEN
) == 0) ||
6346 (!iface_addr
&& psk
->p2p
&&
6347 os_memcmp(addr
, psk
->addr
, ETH_ALEN
) == 0)) {
6348 wpa_dbg(wpa_s
, MSG_DEBUG
,
6349 "P2P: Remove persistent group PSK list entry for "
6351 MAC2STR(psk
->addr
), psk
->p2p
);
6352 dl_list_del(&psk
->list
);
6362 void wpas_p2p_new_psk_cb(struct wpa_supplicant
*wpa_s
, const u8
*mac_addr
,
6363 const u8
*p2p_dev_addr
,
6364 const u8
*psk
, size_t psk_len
)
6366 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
6367 struct wpa_ssid
*persistent
;
6368 struct psk_list_entry
*p
;
6370 if (psk_len
!= sizeof(p
->psk
))
6374 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: New PSK for addr=" MACSTR
6375 " p2p_dev_addr=" MACSTR
,
6376 MAC2STR(mac_addr
), MAC2STR(p2p_dev_addr
));
6377 if (is_zero_ether_addr(p2p_dev_addr
))
6378 p2p_dev_addr
= NULL
;
6380 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: New PSK for addr=" MACSTR
,
6384 if (ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
6385 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: new_psk_cb during group formation");
6386 /* To be added to persistent group once created */
6387 if (wpa_s
->global
->add_psk
== NULL
) {
6388 wpa_s
->global
->add_psk
= os_zalloc(sizeof(*p
));
6389 if (wpa_s
->global
->add_psk
== NULL
)
6392 p
= wpa_s
->global
->add_psk
;
6395 os_memcpy(p
->addr
, p2p_dev_addr
, ETH_ALEN
);
6398 os_memcpy(p
->addr
, mac_addr
, ETH_ALEN
);
6400 os_memcpy(p
->psk
, psk
, psk_len
);
6404 if (ssid
->mode
!= WPAS_MODE_P2P_GO
|| !ssid
->p2p_persistent_group
) {
6405 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Ignore new_psk_cb on not-persistent GO");
6409 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, NULL
, ssid
->ssid
,
6412 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not find persistent group information to store the new PSK");
6416 p
= os_zalloc(sizeof(*p
));
6421 os_memcpy(p
->addr
, p2p_dev_addr
, ETH_ALEN
);
6424 os_memcpy(p
->addr
, mac_addr
, ETH_ALEN
);
6426 os_memcpy(p
->psk
, psk
, psk_len
);
6428 if (dl_list_len(&persistent
->psk_list
) > P2P_MAX_STORED_CLIENTS
) {
6429 struct psk_list_entry
*last
;
6430 last
= dl_list_last(&persistent
->psk_list
,
6431 struct psk_list_entry
, list
);
6432 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove oldest PSK entry for "
6433 MACSTR
" (p2p=%u) to make room for a new one",
6434 MAC2STR(last
->addr
), last
->p2p
);
6435 dl_list_del(&last
->list
);
6439 wpas_p2p_remove_psk_entry(wpa_s
->parent
, persistent
,
6440 p2p_dev_addr
? p2p_dev_addr
: mac_addr
,
6441 p2p_dev_addr
== NULL
);
6443 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add new PSK for p2p_dev_addr="
6444 MACSTR
, MAC2STR(p2p_dev_addr
));
6446 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add new PSK for addr=" MACSTR
,
6449 dl_list_add(&persistent
->psk_list
, &p
->list
);
6451 #ifndef CONFIG_NO_CONFIG_WRITE
6452 if (wpa_s
->parent
->conf
->update_config
&&
6453 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
6454 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
6455 #endif /* CONFIG_NO_CONFIG_WRITE */
6459 static void wpas_p2p_remove_psk(struct wpa_supplicant
*wpa_s
,
6460 struct wpa_ssid
*s
, const u8
*addr
,
6465 res
= wpas_p2p_remove_psk_entry(wpa_s
, s
, addr
, iface_addr
);
6467 #ifndef CONFIG_NO_CONFIG_WRITE
6468 if (wpa_s
->conf
->update_config
&&
6469 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
))
6470 wpa_dbg(wpa_s
, MSG_DEBUG
,
6471 "P2P: Failed to update configuration");
6472 #endif /* CONFIG_NO_CONFIG_WRITE */
6477 static void wpas_p2p_remove_client_go(struct wpa_supplicant
*wpa_s
,
6478 const u8
*peer
, int iface_addr
)
6480 struct hostapd_data
*hapd
;
6481 struct hostapd_wpa_psk
*psk
, *prev
, *rem
;
6482 struct sta_info
*sta
;
6484 if (wpa_s
->ap_iface
== NULL
|| wpa_s
->current_ssid
== NULL
||
6485 wpa_s
->current_ssid
->mode
!= WPAS_MODE_P2P_GO
)
6488 /* Remove per-station PSK entry */
6489 hapd
= wpa_s
->ap_iface
->bss
[0];
6491 psk
= hapd
->conf
->ssid
.wpa_psk
;
6493 if ((iface_addr
&& os_memcmp(peer
, psk
->addr
, ETH_ALEN
) == 0) ||
6495 os_memcmp(peer
, psk
->p2p_dev_addr
, ETH_ALEN
) == 0)) {
6496 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove operating group PSK entry for "
6497 MACSTR
" iface_addr=%d",
6498 MAC2STR(peer
), iface_addr
);
6500 prev
->next
= psk
->next
;
6502 hapd
->conf
->ssid
.wpa_psk
= psk
->next
;
6512 /* Disconnect from group */
6514 sta
= ap_get_sta(hapd
, peer
);
6516 sta
= ap_get_sta_p2p(hapd
, peer
);
6518 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Disconnect peer " MACSTR
6519 " (iface_addr=%d) from group",
6520 MAC2STR(peer
), iface_addr
);
6521 hostapd_drv_sta_deauth(hapd
, sta
->addr
,
6522 WLAN_REASON_DEAUTH_LEAVING
);
6523 ap_sta_deauthenticate(hapd
, sta
, WLAN_REASON_DEAUTH_LEAVING
);
6528 void wpas_p2p_remove_client(struct wpa_supplicant
*wpa_s
, const u8
*peer
,
6532 struct wpa_supplicant
*w
;
6534 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove client " MACSTR
, MAC2STR(peer
));
6536 /* Remove from any persistent group */
6537 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
6538 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
6541 wpas_remove_persistent_peer(wpa_s
, s
, peer
, 0);
6542 wpas_p2p_remove_psk(wpa_s
->parent
, s
, peer
, iface_addr
);
6545 /* Remove from any operating group */
6546 for (w
= wpa_s
->global
->ifaces
; w
; w
= w
->next
)
6547 wpas_p2p_remove_client_go(w
, peer
, iface_addr
);
6551 static void wpas_p2p_psk_failure_removal(void *eloop_ctx
, void *timeout_ctx
)
6553 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
6554 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_PSK_FAILURE
);
6558 static void wpas_p2p_group_freq_conflict(void *eloop_ctx
, void *timeout_ctx
)
6560 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
6562 wpa_printf(MSG_DEBUG
, "P2P: Frequency conflict - terminate group");
6563 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_FREQ_CONFLICT
);
6567 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant
*wpa_s
, int freq
,
6568 struct wpa_ssid
*ssid
)
6570 struct wpa_supplicant
*iface
;
6572 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
6573 if (!iface
->current_ssid
||
6574 iface
->current_ssid
->frequency
== freq
||
6575 (iface
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
&&
6576 !iface
->current_ssid
->p2p_group
))
6579 /* Remove the connection with least priority */
6580 if (!wpas_is_p2p_prioritized(iface
)) {
6581 /* STA connection has priority over existing
6582 * P2P connection, so remove the interface. */
6583 wpa_printf(MSG_DEBUG
, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6584 eloop_register_timeout(0, 0,
6585 wpas_p2p_group_freq_conflict
,
6587 /* If connection in progress is P2P connection, do not
6588 * proceed for the connection. */
6594 /* P2P connection has priority, disable the STA network
6596 wpa_supplicant_disable_network(wpa_s
->global
->ifaces
,
6598 wpa_msg(wpa_s
->global
->ifaces
, MSG_INFO
,
6599 WPA_EVENT_FREQ_CONFLICT
" id=%d", ssid
->id
);
6600 os_memset(wpa_s
->global
->ifaces
->pending_bssid
, 0,
6602 /* If P2P connection is in progress, continue
6615 int wpas_p2p_4way_hs_failed(struct wpa_supplicant
*wpa_s
)
6617 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
6619 if (ssid
== NULL
|| !ssid
->p2p_group
)
6622 if (wpa_s
->p2p_last_4way_hs_fail
&&
6623 wpa_s
->p2p_last_4way_hs_fail
== ssid
) {
6624 u8 go_dev_addr
[ETH_ALEN
];
6625 struct wpa_ssid
*persistent
;
6627 if (wpas_p2p_persistent_group(wpa_s
, go_dev_addr
,
6629 ssid
->ssid_len
) <= 0) {
6630 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6634 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6635 MACSTR
, MAC2STR(go_dev_addr
));
6636 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, go_dev_addr
,
6639 if (persistent
== NULL
|| persistent
->mode
!= WPAS_MODE_INFRA
) {
6640 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No matching persistent group stored");
6643 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
6644 P2P_EVENT_PERSISTENT_PSK_FAIL
"%d",
6647 wpa_s
->p2p_last_4way_hs_fail
= NULL
;
6649 * Remove the group from a timeout to avoid issues with caller
6650 * continuing to use the interface if this is on a P2P group
6653 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal
,
6658 wpa_s
->p2p_last_4way_hs_fail
= ssid
;