3 * Copyright (c) 2009-2010, Atheros Communications
4 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
14 #include "common/ieee802_11_common.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/wpa_ctrl.h"
17 #include "wps/wps_i.h"
19 #include "ap/hostapd.h"
20 #include "ap/ap_config.h"
21 #include "ap/sta_info.h"
22 #include "ap/ap_drv_ops.h"
23 #include "ap/p2p_hostapd.h"
24 #include "eapol_supp/eapol_supp_sm.h"
25 #include "rsn_supp/wpa.h"
26 #include "wpa_supplicant_i.h"
29 #include "config_ssid.h"
34 #include "offchannel.h"
35 #include "wps_supplicant.h"
36 #include "p2p_supplicant.h"
37 #include "wifi_display.h"
41 * How many times to try to scan to find the GO before giving up on join
44 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
46 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
48 #ifndef P2P_MAX_CLIENT_IDLE
50 * How many seconds to try to reconnect to the GO when connection in P2P client
53 #define P2P_MAX_CLIENT_IDLE 10
54 #endif /* P2P_MAX_CLIENT_IDLE */
56 #ifndef P2P_MAX_INITIAL_CONN_WAIT
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
61 #define P2P_MAX_INITIAL_CONN_WAIT 10
62 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
64 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
66 * How many seconds to wait for initial 4-way handshake to get completed after
67 * WPS provisioning step on the GO. This controls the extra time the P2P
68 * operation is considered to be in progress (e.g., to delay other scans) after
69 * WPS provisioning has been completed on the GO during group formation.
71 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
72 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
74 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
76 * How many seconds to wait for initial 4-way handshake to get completed after
77 * re-invocation of a persistent group on the GO when the client is expected
78 * to connect automatically (no user interaction).
80 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
81 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
83 #ifndef P2P_CONCURRENT_SEARCH_DELAY
84 #define P2P_CONCURRENT_SEARCH_DELAY 500
85 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
87 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
89 enum p2p_group_removal_reason
{
90 P2P_GROUP_REMOVAL_UNKNOWN
,
91 P2P_GROUP_REMOVAL_SILENT
,
92 P2P_GROUP_REMOVAL_FORMATION_FAILED
,
93 P2P_GROUP_REMOVAL_REQUESTED
,
94 P2P_GROUP_REMOVAL_IDLE_TIMEOUT
,
95 P2P_GROUP_REMOVAL_UNAVAILABLE
,
96 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
,
97 P2P_GROUP_REMOVAL_PSK_FAILURE
,
98 P2P_GROUP_REMOVAL_FREQ_CONFLICT
102 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
103 static struct wpa_supplicant
*
104 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
106 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
);
107 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
,
108 const u8
*ssid
, size_t ssid_len
);
109 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
);
110 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
111 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
112 int auto_join
, const u8
*ssid
, size_t ssid_len
);
113 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
);
114 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
);
115 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
);
116 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
);
117 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
119 static void wpas_p2p_group_freq_conflict(void *eloop_ctx
, void *timeout_ctx
);
120 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
122 static int wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
);
126 * Get the number of concurrent channels that the HW can operate, but that are
127 * currently not in use by any of the wpa_supplicant interfaces.
129 static int wpas_p2p_num_unused_channels(struct wpa_supplicant
*wpa_s
)
134 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
138 num
= get_shared_radio_freqs(wpa_s
, freqs
,
139 wpa_s
->num_multichan_concurrent
);
142 unused
= wpa_s
->num_multichan_concurrent
- num
;
143 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: num_unused_channels: %d", unused
);
149 * Get the frequencies that are currently in use by one or more of the virtual
150 * interfaces, and that are also valid for P2P operation.
152 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant
*wpa_s
,
153 int *p2p_freqs
, unsigned int len
)
156 unsigned int num
, i
, j
;
158 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
162 num
= get_shared_radio_freqs(wpa_s
, freqs
,
163 wpa_s
->num_multichan_concurrent
);
165 os_memset(p2p_freqs
, 0, sizeof(int) * len
);
167 for (i
= 0, j
= 0; i
< num
&& j
< len
; i
++) {
168 if (p2p_supported_freq(wpa_s
->global
->p2p
, freqs
[i
]))
169 p2p_freqs
[j
++] = freqs
[i
];
174 dump_freq_array(wpa_s
, "valid for P2P", p2p_freqs
, j
);
180 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant
*wpa_s
,
183 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
185 if (wpa_s
->parent
->conf
->p2p_ignore_shared_freq
&&
186 freq
> 0 && wpa_s
->num_multichan_concurrent
> 1 &&
187 wpas_p2p_num_unused_channels(wpa_s
) > 0) {
188 wpa_printf(MSG_DEBUG
, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
192 p2p_set_own_freq_preference(wpa_s
->global
->p2p
, freq
);
196 static void wpas_p2p_scan_res_handler(struct wpa_supplicant
*wpa_s
,
197 struct wpa_scan_results
*scan_res
)
201 if (wpa_s
->p2p_scan_work
) {
202 struct wpa_radio_work
*work
= wpa_s
->p2p_scan_work
;
203 wpa_s
->p2p_scan_work
= NULL
;
204 radio_work_done(work
);
207 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
210 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS)",
211 (int) scan_res
->num
);
213 for (i
= 0; i
< scan_res
->num
; i
++) {
214 struct wpa_scan_res
*bss
= scan_res
->res
[i
];
215 struct os_reltime time_tmp_age
, entry_ts
;
219 time_tmp_age
.sec
= bss
->age
/ 1000;
220 time_tmp_age
.usec
= (bss
->age
% 1000) * 1000;
221 os_reltime_sub(&scan_res
->fetch_time
, &time_tmp_age
, &entry_ts
);
223 ies
= (const u8
*) (bss
+ 1);
224 ies_len
= bss
->ie_len
;
225 if (bss
->beacon_ie_len
> 0 &&
226 !wpa_scan_get_vendor_ie(bss
, P2P_IE_VENDOR_TYPE
) &&
227 wpa_scan_get_vendor_ie_beacon(bss
, P2P_IE_VENDOR_TYPE
)) {
228 wpa_printf(MSG_DEBUG
, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
229 MACSTR
, MAC2STR(bss
->bssid
));
231 ies_len
= bss
->beacon_ie_len
;
235 if (p2p_scan_res_handler(wpa_s
->global
->p2p
, bss
->bssid
,
236 bss
->freq
, &entry_ts
, bss
->level
,
241 p2p_scan_res_handled(wpa_s
->global
->p2p
);
245 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work
*work
, int deinit
)
247 struct wpa_supplicant
*wpa_s
= work
->wpa_s
;
248 struct wpa_driver_scan_params
*params
= work
->ctx
;
252 wpa_scan_free_params(params
);
256 ret
= wpa_drv_scan(wpa_s
, params
);
257 wpa_scan_free_params(params
);
260 radio_work_done(work
);
264 os_get_reltime(&wpa_s
->scan_trigger_time
);
265 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
266 wpa_s
->own_scan_requested
= 1;
267 wpa_s
->p2p_scan_work
= work
;
271 static int wpas_p2p_scan(void *ctx
, enum p2p_scan_type type
, int freq
,
272 unsigned int num_req_dev_types
,
273 const u8
*req_dev_types
, const u8
*dev_id
, u16 pw_id
)
275 struct wpa_supplicant
*wpa_s
= ctx
;
276 struct wpa_driver_scan_params
*params
= NULL
;
277 struct wpabuf
*wps_ie
, *ies
;
281 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
284 if (wpa_s
->p2p_scan_work
) {
285 wpa_dbg(wpa_s
, MSG_INFO
, "P2P: Reject scan trigger since one is already pending");
289 params
= os_zalloc(sizeof(*params
));
293 /* P2P Wildcard SSID */
294 params
->num_ssids
= 1;
295 n
= os_malloc(P2P_WILDCARD_SSID_LEN
);
298 os_memcpy(n
, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
);
299 params
->ssids
[0].ssid
= n
;
300 params
->ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
302 wpa_s
->wps
->dev
.p2p
= 1;
303 wps_ie
= wps_build_probe_req_ie(pw_id
, &wpa_s
->wps
->dev
,
304 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
,
305 num_req_dev_types
, req_dev_types
);
309 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
310 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
315 wpabuf_put_buf(ies
, wps_ie
);
318 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, dev_id
);
320 params
->p2p_probe
= 1;
321 n
= os_malloc(wpabuf_len(ies
));
326 os_memcpy(n
, wpabuf_head(ies
), wpabuf_len(ies
));
327 params
->extra_ies
= n
;
328 params
->extra_ies_len
= wpabuf_len(ies
);
332 case P2P_SCAN_SOCIAL
:
333 params
->freqs
= os_malloc(4 * sizeof(int));
334 if (params
->freqs
== NULL
)
336 params
->freqs
[0] = 2412;
337 params
->freqs
[1] = 2437;
338 params
->freqs
[2] = 2462;
339 params
->freqs
[3] = 0;
343 case P2P_SCAN_SOCIAL_PLUS_ONE
:
344 params
->freqs
= os_malloc(5 * sizeof(int));
345 if (params
->freqs
== NULL
)
347 params
->freqs
[0] = 2412;
348 params
->freqs
[1] = 2437;
349 params
->freqs
[2] = 2462;
350 params
->freqs
[3] = freq
;
351 params
->freqs
[4] = 0;
355 radio_remove_unstarted_work(wpa_s
, "p2p-scan");
356 if (radio_add_work(wpa_s
, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb
,
362 wpa_scan_free_params(params
);
367 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
369 switch (p2p_group_interface
) {
370 case P2P_GROUP_INTERFACE_PENDING
:
371 return WPA_IF_P2P_GROUP
;
372 case P2P_GROUP_INTERFACE_GO
:
373 return WPA_IF_P2P_GO
;
374 case P2P_GROUP_INTERFACE_CLIENT
:
375 return WPA_IF_P2P_CLIENT
;
378 return WPA_IF_P2P_GROUP
;
382 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
384 size_t ssid_len
, int *go
)
388 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
389 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
390 if (s
->disabled
!= 0 || !s
->p2p_group
||
391 s
->ssid_len
!= ssid_len
||
392 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
394 if (s
->mode
== WPAS_MODE_P2P_GO
&&
395 s
!= wpa_s
->current_ssid
)
398 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
407 static int wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
,
408 enum p2p_group_removal_reason removal_reason
)
410 struct wpa_ssid
*ssid
;
414 ssid
= wpa_s
->current_ssid
;
417 * The current SSID was not known, but there may still be a
418 * pending P2P group interface waiting for provisioning or a
419 * P2P group that is trying to reconnect.
421 ssid
= wpa_s
->conf
->ssid
;
423 if (ssid
->p2p_group
&& ssid
->disabled
!= 2)
428 wpa_s
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)
430 wpa_printf(MSG_ERROR
, "P2P: P2P group interface "
435 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
437 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
438 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
439 wpa_s
->reassociate
= 0;
440 wpa_s
->disconnected
= 1;
441 wpa_supplicant_deauthenticate(wpa_s
,
442 WLAN_REASON_DEAUTH_LEAVING
);
446 if (wpa_s
->cross_connect_in_use
) {
447 wpa_s
->cross_connect_in_use
= 0;
448 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
449 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
450 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
452 switch (removal_reason
) {
453 case P2P_GROUP_REMOVAL_REQUESTED
:
454 reason
= " reason=REQUESTED";
456 case P2P_GROUP_REMOVAL_FORMATION_FAILED
:
457 reason
= " reason=FORMATION_FAILED";
459 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
460 reason
= " reason=IDLE";
462 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
463 reason
= " reason=UNAVAILABLE";
465 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION
:
466 reason
= " reason=GO_ENDING_SESSION";
468 case P2P_GROUP_REMOVAL_PSK_FAILURE
:
469 reason
= " reason=PSK_FAILURE";
471 case P2P_GROUP_REMOVAL_FREQ_CONFLICT
:
472 reason
= " reason=FREQ_CONFLICT";
478 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
) {
479 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
480 P2P_EVENT_GROUP_REMOVED
"%s %s%s",
481 wpa_s
->ifname
, gtype
, reason
);
484 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict
, wpa_s
, NULL
) > 0)
485 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group freq_conflict timeout");
486 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
487 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
488 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
489 wpa_s
->parent
, NULL
) > 0) {
490 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group formation "
492 wpa_s
->p2p_in_provisioning
= 0;
496 * Make sure wait for the first client does not remain active after the
497 * group has been removed.
499 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
501 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
&& ssid
)
502 wpas_notify_p2p_group_removed(wpa_s
, ssid
, gtype
);
504 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
505 struct wpa_global
*global
;
507 enum wpa_driver_if_type type
;
508 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
510 global
= wpa_s
->global
;
511 ifname
= os_strdup(wpa_s
->ifname
);
512 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
513 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
, 0);
514 wpa_s
= global
->ifaces
;
516 wpa_drv_if_remove(wpa_s
, type
, ifname
);
521 if (!wpa_s
->p2p_go_group_formation_completed
) {
522 wpa_s
->global
->p2p_group_formation
= NULL
;
523 wpa_s
->p2p_in_provisioning
= 0;
526 wpa_s
->show_group_started
= 0;
527 os_free(wpa_s
->go_params
);
528 wpa_s
->go_params
= NULL
;
530 wpa_s
->waiting_presence_resp
= 0;
532 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
533 if (ssid
&& (ssid
->p2p_group
||
534 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
535 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
537 if (ssid
== wpa_s
->current_ssid
) {
538 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
539 eapol_sm_notify_config(wpa_s
->eapol
, NULL
, NULL
);
540 wpa_s
->current_ssid
= NULL
;
543 * Networks objects created during any P2P activities are not
544 * exposed out as they might/will confuse certain non-P2P aware
545 * applications since these network objects won't behave like
548 * Likewise, we don't send out network removed signals for such
551 wpa_config_remove_network(wpa_s
->conf
, id
);
552 wpa_supplicant_clear_status(wpa_s
);
553 wpa_supplicant_cancel_sched_scan(wpa_s
);
555 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
559 wpa_supplicant_ap_deinit(wpa_s
);
561 wpa_drv_deinit_p2p_cli(wpa_s
);
567 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
569 const u8
*ssid
, size_t ssid_len
)
577 if (wpa_s
->go_params
)
578 bssid
= wpa_s
->go_params
->peer_interface_addr
;
580 bssid
= wpa_s
->bssid
;
582 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
584 u8 iface_addr
[ETH_ALEN
];
585 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
587 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
590 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
591 "group is persistent - BSS " MACSTR
" not found",
596 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
598 p2p
= wpa_bss_get_vendor_ie_multi_beacon(bss
,
601 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
602 "group is persistent - BSS " MACSTR
603 " did not include P2P IE", MAC2STR(bssid
));
604 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
605 (u8
*) (bss
+ 1), bss
->ie_len
);
606 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
607 ((u8
*) bss
+ 1) + bss
->ie_len
,
612 group_capab
= p2p_get_group_capab(p2p
);
613 addr
= p2p_get_go_dev_addr(p2p
);
614 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
615 "group_capab=0x%x", group_capab
);
617 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
618 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
621 os_memset(go_dev_addr
, 0, ETH_ALEN
);
624 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
625 "go_dev_addr=" MACSTR
,
626 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
628 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
632 static int wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
633 struct wpa_ssid
*ssid
,
634 const u8
*go_dev_addr
)
639 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
640 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
641 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
642 if (s
->disabled
== 2 &&
643 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
644 s
->ssid_len
== ssid
->ssid_len
&&
645 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
650 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
652 if (ssid
->passphrase
&& !s
->passphrase
)
654 else if (ssid
->passphrase
&& s
->passphrase
&&
655 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
658 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
661 s
= wpa_config_add_network(wpa_s
->conf
);
666 * Instead of network_added we emit persistent_group_added
667 * notification. Also to keep the defense checks in
668 * persistent_group obj registration method, we set the
669 * relevant flags in s to designate it as a persistent group.
672 s
->p2p_persistent_group
= 1;
673 wpas_notify_persistent_group_added(wpa_s
, s
);
674 wpa_config_set_network_defaults(s
);
678 s
->p2p_persistent_group
= 1;
681 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
682 s
->mode
= ssid
->mode
;
683 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
684 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
685 s
->proto
= WPA_PROTO_RSN
;
686 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
688 if (ssid
->passphrase
) {
689 os_free(s
->passphrase
);
690 s
->passphrase
= os_strdup(ssid
->passphrase
);
694 os_memcpy(s
->psk
, ssid
->psk
, 32);
696 if (s
->passphrase
&& !s
->psk_set
)
697 wpa_config_update_psk(s
);
698 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
700 s
->ssid
= os_malloc(ssid
->ssid_len
);
703 s
->ssid_len
= ssid
->ssid_len
;
704 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
706 if (ssid
->mode
== WPAS_MODE_P2P_GO
&& wpa_s
->global
->add_psk
) {
707 dl_list_add(&s
->psk_list
, &wpa_s
->global
->add_psk
->list
);
708 wpa_s
->global
->add_psk
= NULL
;
712 #ifndef CONFIG_NO_CONFIG_WRITE
713 if (changed
&& wpa_s
->conf
->update_config
&&
714 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
715 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
717 #endif /* CONFIG_NO_CONFIG_WRITE */
723 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant
*wpa_s
,
726 struct wpa_ssid
*ssid
, *s
;
731 ssid
= wpa_s
->current_ssid
;
732 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
733 !ssid
->p2p_persistent_group
)
736 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
737 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
740 if (s
->ssid_len
== ssid
->ssid_len
&&
741 os_memcmp(s
->ssid
, ssid
->ssid
, s
->ssid_len
) == 0)
748 for (i
= 0; s
->p2p_client_list
&& i
< s
->num_p2p_clients
; i
++) {
749 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
, addr
,
753 if (i
== s
->num_p2p_clients
- 1)
754 return; /* already the most recent entry */
756 /* move the entry to mark it most recent */
757 os_memmove(s
->p2p_client_list
+ i
* ETH_ALEN
,
758 s
->p2p_client_list
+ (i
+ 1) * ETH_ALEN
,
759 (s
->num_p2p_clients
- i
- 1) * ETH_ALEN
);
760 os_memcpy(s
->p2p_client_list
+
761 (s
->num_p2p_clients
- 1) * ETH_ALEN
, addr
, ETH_ALEN
);
766 if (!found
&& s
->num_p2p_clients
< P2P_MAX_STORED_CLIENTS
) {
767 n
= os_realloc_array(s
->p2p_client_list
,
768 s
->num_p2p_clients
+ 1, ETH_ALEN
);
771 os_memcpy(n
+ s
->num_p2p_clients
* ETH_ALEN
, addr
, ETH_ALEN
);
772 s
->p2p_client_list
= n
;
773 s
->num_p2p_clients
++;
775 /* Not enough room for an additional entry - drop the oldest
777 os_memmove(s
->p2p_client_list
,
778 s
->p2p_client_list
+ ETH_ALEN
,
779 (s
->num_p2p_clients
- 1) * ETH_ALEN
);
780 os_memcpy(s
->p2p_client_list
+
781 (s
->num_p2p_clients
- 1) * ETH_ALEN
,
785 #ifndef CONFIG_NO_CONFIG_WRITE
786 if (wpa_s
->parent
->conf
->update_config
&&
787 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
788 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
789 #endif /* CONFIG_NO_CONFIG_WRITE */
793 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
796 struct wpa_ssid
*ssid
;
797 const char *ssid_txt
;
800 u8 go_dev_addr
[ETH_ALEN
];
804 * This callback is likely called for the main interface. Update wpa_s
805 * to use the group interface if a new interface was created for the
808 if (wpa_s
->global
->p2p_group_formation
)
809 wpa_s
= wpa_s
->global
->p2p_group_formation
;
810 if (wpa_s
->p2p_go_group_formation_completed
) {
811 wpa_s
->global
->p2p_group_formation
= NULL
;
812 wpa_s
->p2p_in_provisioning
= 0;
816 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
817 P2P_EVENT_GROUP_FORMATION_FAILURE
);
818 wpas_p2p_group_delete(wpa_s
,
819 P2P_GROUP_REMOVAL_FORMATION_FAILED
);
823 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
824 P2P_EVENT_GROUP_FORMATION_SUCCESS
);
826 ssid
= wpa_s
->current_ssid
;
827 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
828 ssid
->mode
= WPAS_MODE_P2P_GO
;
829 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
830 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
835 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
836 client
= ssid
->mode
== WPAS_MODE_INFRA
;
837 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
838 persistent
= ssid
->p2p_persistent_group
;
839 os_memcpy(go_dev_addr
, wpa_s
->global
->p2p_dev_addr
,
842 persistent
= wpas_p2p_persistent_group(wpa_s
,
848 client
= wpa_s
->p2p_group_interface
==
849 P2P_GROUP_INTERFACE_CLIENT
;
850 os_memset(go_dev_addr
, 0, ETH_ALEN
);
853 wpa_s
->show_group_started
= 0;
856 * Indicate event only after successfully completed 4-way
857 * handshake, i.e., when the interface is ready for data
860 wpa_s
->show_group_started
= 1;
861 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
863 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
864 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
865 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
867 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
868 MAC2STR(go_dev_addr
),
869 persistent
? " [PERSISTENT]" : "");
870 wpas_p2p_cross_connect_setup(wpa_s
);
871 wpas_p2p_set_group_idle_timeout(wpa_s
);
873 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
874 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
875 "go_dev_addr=" MACSTR
"%s",
876 wpa_s
->ifname
, ssid_txt
,
877 ssid
? ssid
->frequency
: 0,
878 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
879 MAC2STR(go_dev_addr
),
880 persistent
? " [PERSISTENT]" : "");
881 wpas_p2p_cross_connect_setup(wpa_s
);
882 wpas_p2p_set_group_idle_timeout(wpa_s
);
886 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
889 os_free(wpa_s
->global
->add_psk
);
890 wpa_s
->global
->add_psk
= NULL
;
892 if (network_id
< 0 && ssid
)
893 network_id
= ssid
->id
;
895 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
896 os_get_reltime(&wpa_s
->global
->p2p_go_wait_client
);
901 struct send_action_work
{
907 unsigned int wait_time
;
912 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx
,
915 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
917 if (!wpa_s
->p2p_send_action_work
)
920 wpa_printf(MSG_DEBUG
, "P2P: Send Action frame radio work timed out");
921 os_free(wpa_s
->p2p_send_action_work
->ctx
);
922 radio_work_done(wpa_s
->p2p_send_action_work
);
923 wpa_s
->p2p_send_action_work
= NULL
;
927 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant
*wpa_s
,
929 const u8
*dst
, const u8
*src
,
931 const u8
*data
, size_t data_len
,
932 enum offchannel_send_action_result
935 enum p2p_send_action_result res
= P2P_SEND_ACTION_SUCCESS
;
937 if (wpa_s
->p2p_send_action_work
) {
938 struct send_action_work
*awork
;
939 awork
= wpa_s
->p2p_send_action_work
->ctx
;
940 if (awork
->wait_time
== 0) {
942 radio_work_done(wpa_s
->p2p_send_action_work
);
943 wpa_s
->p2p_send_action_work
= NULL
;
946 * In theory, this should not be needed, but number of
947 * places in the P2P code is still using non-zero wait
948 * time for the last Action frame in the sequence and
949 * some of these do not call send_action_done().
951 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout
,
953 eloop_register_timeout(
954 0, awork
->wait_time
* 1000,
955 wpas_p2p_send_action_work_timeout
,
960 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
964 case OFFCHANNEL_SEND_ACTION_SUCCESS
:
965 res
= P2P_SEND_ACTION_SUCCESS
;
967 case OFFCHANNEL_SEND_ACTION_NO_ACK
:
968 res
= P2P_SEND_ACTION_NO_ACK
;
970 case OFFCHANNEL_SEND_ACTION_FAILED
:
971 res
= P2P_SEND_ACTION_FAILED
;
975 p2p_send_action_cb(wpa_s
->global
->p2p
, freq
, dst
, src
, bssid
, res
);
977 if (result
!= OFFCHANNEL_SEND_ACTION_SUCCESS
&&
978 wpa_s
->pending_pd_before_join
&&
979 (os_memcmp(dst
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
980 os_memcmp(dst
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0) &&
981 wpa_s
->p2p_fallback_to_go_neg
) {
982 wpa_s
->pending_pd_before_join
= 0;
983 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No ACK for PD Req "
984 "during p2p_connect-auto");
985 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
991 static void wpas_send_action_cb(struct wpa_radio_work
*work
, int deinit
)
993 struct wpa_supplicant
*wpa_s
= work
->wpa_s
;
994 struct send_action_work
*awork
= work
->ctx
;
1001 if (offchannel_send_action(wpa_s
, awork
->freq
, awork
->dst
, awork
->src
,
1002 awork
->bssid
, awork
->buf
, awork
->len
,
1004 wpas_p2p_send_action_tx_status
, 1) < 0) {
1006 radio_work_done(work
);
1009 wpa_s
->p2p_send_action_work
= work
;
1013 static int wpas_send_action_work(struct wpa_supplicant
*wpa_s
,
1014 unsigned int freq
, const u8
*dst
,
1015 const u8
*src
, const u8
*bssid
, const u8
*buf
,
1016 size_t len
, unsigned int wait_time
)
1018 struct send_action_work
*awork
;
1020 if (wpa_s
->p2p_send_action_work
) {
1021 wpa_printf(MSG_DEBUG
, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1025 awork
= os_zalloc(sizeof(*awork
) + len
);
1030 os_memcpy(awork
->dst
, dst
, ETH_ALEN
);
1031 os_memcpy(awork
->src
, src
, ETH_ALEN
);
1032 os_memcpy(awork
->bssid
, bssid
, ETH_ALEN
);
1034 awork
->wait_time
= wait_time
;
1035 os_memcpy(awork
->buf
, buf
, len
);
1037 if (radio_add_work(wpa_s
, freq
, "p2p-send-action", 0,
1038 wpas_send_action_cb
, awork
) < 0) {
1047 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
1048 const u8
*src
, const u8
*bssid
, const u8
*buf
,
1049 size_t len
, unsigned int wait_time
)
1051 struct wpa_supplicant
*wpa_s
= ctx
;
1052 int listen_freq
= -1, send_freq
= -1;
1054 if (wpa_s
->p2p_listen_work
)
1055 listen_freq
= wpa_s
->p2p_listen_work
->freq
;
1056 if (wpa_s
->p2p_send_action_work
)
1057 send_freq
= wpa_s
->p2p_send_action_work
->freq
;
1058 if (listen_freq
!= (int) freq
&& send_freq
!= (int) freq
) {
1059 wpa_printf(MSG_DEBUG
, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1060 listen_freq
, send_freq
);
1061 return wpas_send_action_work(wpa_s
, freq
, dst
, src
, bssid
, buf
,
1065 wpa_printf(MSG_DEBUG
, "P2P: Use ongoing radio work for Action frame TX");
1066 return offchannel_send_action(wpa_s
, freq
, dst
, src
, bssid
, buf
, len
,
1068 wpas_p2p_send_action_tx_status
, 1);
1072 static void wpas_send_action_done(void *ctx
)
1074 struct wpa_supplicant
*wpa_s
= ctx
;
1076 if (wpa_s
->p2p_send_action_work
) {
1077 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout
,
1079 os_free(wpa_s
->p2p_send_action_work
->ctx
);
1080 radio_work_done(wpa_s
->p2p_send_action_work
);
1081 wpa_s
->p2p_send_action_work
= NULL
;
1084 offchannel_send_action_done(wpa_s
);
1088 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
1089 struct p2p_go_neg_results
*params
)
1091 if (wpa_s
->go_params
== NULL
) {
1092 wpa_s
->go_params
= os_malloc(sizeof(*params
));
1093 if (wpa_s
->go_params
== NULL
)
1096 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
1101 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
1102 struct p2p_go_neg_results
*res
)
1104 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
1105 MAC2STR(res
->peer_interface_addr
));
1106 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
1107 res
->ssid
, res
->ssid_len
);
1108 wpa_supplicant_ap_deinit(wpa_s
);
1109 wpas_copy_go_neg_results(wpa_s
, res
);
1110 if (res
->wps_method
== WPS_PBC
)
1111 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
1113 u16 dev_pw_id
= DEV_PW_DEFAULT
;
1114 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
1115 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
1116 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
1117 wpa_s
->p2p_pin
, 1, dev_pw_id
);
1122 static void wpas_p2p_add_psk_list(struct wpa_supplicant
*wpa_s
,
1123 struct wpa_ssid
*ssid
)
1125 struct wpa_ssid
*persistent
;
1126 struct psk_list_entry
*psk
;
1127 struct hostapd_data
*hapd
;
1129 if (!wpa_s
->ap_iface
)
1132 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, NULL
, ssid
->ssid
,
1134 if (persistent
== NULL
)
1137 hapd
= wpa_s
->ap_iface
->bss
[0];
1139 dl_list_for_each(psk
, &persistent
->psk_list
, struct psk_list_entry
,
1141 struct hostapd_wpa_psk
*hpsk
;
1143 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add persistent group PSK entry for "
1145 MAC2STR(psk
->addr
), psk
->p2p
);
1146 hpsk
= os_zalloc(sizeof(*hpsk
));
1149 os_memcpy(hpsk
->psk
, psk
->psk
, PMK_LEN
);
1151 os_memcpy(hpsk
->p2p_dev_addr
, psk
->addr
, ETH_ALEN
);
1153 os_memcpy(hpsk
->addr
, psk
->addr
, ETH_ALEN
);
1154 hpsk
->next
= hapd
->conf
->ssid
.wpa_psk
;
1155 hapd
->conf
->ssid
.wpa_psk
= hpsk
;
1160 static void p2p_go_configured(void *ctx
, void *data
)
1162 struct wpa_supplicant
*wpa_s
= ctx
;
1163 struct p2p_go_neg_results
*params
= data
;
1164 struct wpa_ssid
*ssid
;
1165 int network_id
= -1;
1167 ssid
= wpa_s
->current_ssid
;
1168 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
1169 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
1170 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
1171 wpa_s
->global
->p2p_group_formation
= NULL
;
1172 if (os_strlen(params
->passphrase
) > 0) {
1173 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
1174 P2P_EVENT_GROUP_STARTED
1175 "%s GO ssid=\"%s\" freq=%d "
1176 "passphrase=\"%s\" go_dev_addr=" MACSTR
1177 "%s", wpa_s
->ifname
,
1178 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
1179 ssid
->frequency
, params
->passphrase
,
1180 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
1181 params
->persistent_group
?
1182 " [PERSISTENT]" : "");
1185 wpa_snprintf_hex(psk
, sizeof(psk
), params
->psk
,
1186 sizeof(params
->psk
));
1187 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
1188 P2P_EVENT_GROUP_STARTED
1189 "%s GO ssid=\"%s\" freq=%d psk=%s "
1190 "go_dev_addr=" MACSTR
"%s",
1192 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
1193 ssid
->frequency
, psk
,
1194 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
1195 params
->persistent_group
?
1196 " [PERSISTENT]" : "");
1199 os_get_reltime(&wpa_s
->global
->p2p_go_wait_client
);
1200 if (params
->persistent_group
) {
1201 network_id
= wpas_p2p_store_persistent_group(
1202 wpa_s
->parent
, ssid
,
1203 wpa_s
->global
->p2p_dev_addr
);
1204 wpas_p2p_add_psk_list(wpa_s
, ssid
);
1207 network_id
= ssid
->id
;
1208 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
1209 wpas_p2p_cross_connect_setup(wpa_s
);
1210 wpas_p2p_set_group_idle_timeout(wpa_s
);
1212 if (wpa_s
->p2p_first_connection_timeout
) {
1213 wpa_dbg(wpa_s
, MSG_DEBUG
,
1214 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1215 wpa_s
->p2p_first_connection_timeout
);
1216 wpa_s
->p2p_go_group_formation_completed
= 0;
1217 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1218 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
1219 wpa_s
->parent
, NULL
);
1220 eloop_register_timeout(
1221 wpa_s
->p2p_first_connection_timeout
, 0,
1222 wpas_p2p_group_formation_timeout
,
1223 wpa_s
->parent
, NULL
);
1229 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
1230 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
1231 params
->peer_interface_addr
)) {
1232 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
1236 if (params
->wps_method
== WPS_PBC
)
1237 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
1238 params
->peer_device_addr
);
1239 else if (wpa_s
->p2p_pin
[0])
1240 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
1241 wpa_s
->p2p_pin
, NULL
, 0, 0);
1242 os_free(wpa_s
->go_params
);
1243 wpa_s
->go_params
= NULL
;
1247 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
1248 struct p2p_go_neg_results
*params
,
1249 int group_formation
)
1251 struct wpa_ssid
*ssid
;
1253 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Starting GO");
1254 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0) {
1255 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not copy GO Negotiation "
1260 ssid
= wpa_config_add_network(wpa_s
->conf
);
1262 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not add network for GO");
1266 wpa_s
->show_group_started
= 0;
1268 wpa_config_set_network_defaults(ssid
);
1269 ssid
->temporary
= 1;
1270 ssid
->p2p_group
= 1;
1271 ssid
->p2p_persistent_group
= params
->persistent_group
;
1272 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
1274 ssid
->frequency
= params
->freq
;
1275 ssid
->ht40
= params
->ht40
;
1276 ssid
->vht
= params
->vht
;
1277 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
1279 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
1280 ssid
->ssid_len
= params
->ssid_len
;
1282 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
1283 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
1284 ssid
->proto
= WPA_PROTO_RSN
;
1285 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
1286 if (os_strlen(params
->passphrase
) > 0) {
1287 ssid
->passphrase
= os_strdup(params
->passphrase
);
1288 if (ssid
->passphrase
== NULL
) {
1289 wpa_msg_global(wpa_s
, MSG_ERROR
,
1290 "P2P: Failed to copy passphrase for GO");
1291 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1295 ssid
->passphrase
= NULL
;
1296 ssid
->psk_set
= params
->psk_set
;
1298 os_memcpy(ssid
->psk
, params
->psk
, sizeof(ssid
->psk
));
1299 else if (ssid
->passphrase
)
1300 wpa_config_update_psk(ssid
);
1301 ssid
->ap_max_inactivity
= wpa_s
->parent
->conf
->p2p_go_max_inactivity
;
1303 wpa_s
->ap_configured_cb
= p2p_go_configured
;
1304 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
1305 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
1306 wpa_s
->connect_without_scan
= ssid
;
1307 wpa_s
->reassociate
= 1;
1308 wpa_s
->disconnected
= 0;
1309 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Request scan (that will be skipped) to "
1311 wpa_supplicant_req_scan(wpa_s
, 0, 0);
1315 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
1316 const struct wpa_supplicant
*src
)
1318 struct wpa_config
*d
;
1319 const struct wpa_config
*s
;
1324 #define C(n) if (s->n) d->n = os_strdup(s->n)
1333 os_memcpy(d
->device_type
, s
->device_type
, WPS_DEV_TYPE_LEN
);
1334 os_memcpy(d
->sec_device_type
, s
->sec_device_type
,
1335 sizeof(d
->sec_device_type
));
1336 d
->num_sec_device_types
= s
->num_sec_device_types
;
1338 d
->p2p_group_idle
= s
->p2p_group_idle
;
1339 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
1340 d
->persistent_reconnect
= s
->persistent_reconnect
;
1341 d
->max_num_sta
= s
->max_num_sta
;
1342 d
->pbc_in_m1
= s
->pbc_in_m1
;
1343 d
->ignore_old_scan_res
= s
->ignore_old_scan_res
;
1344 d
->beacon_int
= s
->beacon_int
;
1345 d
->disassoc_low_ack
= s
->disassoc_low_ack
;
1346 d
->disable_scan_offload
= s
->disable_scan_offload
;
1350 static void wpas_p2p_get_group_ifname(struct wpa_supplicant
*wpa_s
,
1351 char *ifname
, size_t len
)
1353 char *ifname_ptr
= wpa_s
->ifname
;
1355 if (os_strncmp(wpa_s
->ifname
, P2P_MGMT_DEVICE_PREFIX
,
1356 os_strlen(P2P_MGMT_DEVICE_PREFIX
)) == 0) {
1357 ifname_ptr
= os_strrchr(wpa_s
->ifname
, '-') + 1;
1360 os_snprintf(ifname
, len
, "p2p-%s-%d", ifname_ptr
, wpa_s
->p2p_group_idx
);
1361 if (os_strlen(ifname
) >= IFNAMSIZ
&&
1362 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
1363 /* Try to avoid going over the IFNAMSIZ length limit */
1364 os_snprintf(ifname
, len
, "p2p-%d", wpa_s
->p2p_group_idx
);
1369 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
1370 enum wpa_driver_if_type type
)
1372 char ifname
[120], force_ifname
[120];
1374 if (wpa_s
->pending_interface_name
[0]) {
1375 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
1376 "- skip creation of a new one");
1377 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
1378 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
1379 "unknown?! ifname='%s'",
1380 wpa_s
->pending_interface_name
);
1386 wpas_p2p_get_group_ifname(wpa_s
, ifname
, sizeof(ifname
));
1387 force_ifname
[0] = '\0';
1389 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
1391 wpa_s
->p2p_group_idx
++;
1393 wpa_s
->pending_interface_type
= type
;
1394 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
1395 wpa_s
->pending_interface_addr
, NULL
) < 0) {
1396 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
1401 if (force_ifname
[0]) {
1402 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
1404 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
1405 sizeof(wpa_s
->pending_interface_name
));
1407 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
1408 sizeof(wpa_s
->pending_interface_name
));
1409 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
1410 MACSTR
, wpa_s
->pending_interface_name
,
1411 MAC2STR(wpa_s
->pending_interface_addr
));
1417 static void wpas_p2p_remove_pending_group_interface(
1418 struct wpa_supplicant
*wpa_s
)
1420 if (!wpa_s
->pending_interface_name
[0] ||
1421 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
1422 return; /* No pending virtual interface */
1424 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
1425 wpa_s
->pending_interface_name
);
1426 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
1427 wpa_s
->pending_interface_name
);
1428 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1429 wpa_s
->pending_interface_name
[0] = '\0';
1433 static struct wpa_supplicant
*
1434 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1436 struct wpa_interface iface
;
1437 struct wpa_supplicant
*group_wpa_s
;
1439 if (!wpa_s
->pending_interface_name
[0]) {
1440 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1441 if (!wpas_p2p_create_iface(wpa_s
))
1444 * Something has forced us to remove the pending interface; try
1445 * to create a new one and hope for the best that we will get
1446 * the same local address.
1448 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1449 WPA_IF_P2P_CLIENT
) < 0)
1453 os_memset(&iface
, 0, sizeof(iface
));
1454 iface
.ifname
= wpa_s
->pending_interface_name
;
1455 iface
.driver
= wpa_s
->driver
->name
;
1456 if (wpa_s
->conf
->ctrl_interface
== NULL
&&
1457 wpa_s
->parent
!= wpa_s
&&
1459 (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE
))
1460 iface
.ctrl_interface
= wpa_s
->parent
->conf
->ctrl_interface
;
1462 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1463 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1464 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1465 if (group_wpa_s
== NULL
) {
1466 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1467 "wpa_supplicant interface");
1470 wpa_s
->pending_interface_name
[0] = '\0';
1471 group_wpa_s
->parent
= wpa_s
;
1472 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1473 P2P_GROUP_INTERFACE_CLIENT
;
1474 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1476 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
1482 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
1485 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1486 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
1487 wpas_p2p_group_formation_failed(wpa_s
);
1491 void wpas_p2p_group_formation_failed(struct wpa_supplicant
*wpa_s
)
1493 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
1494 wpa_s
->parent
, NULL
);
1495 if (wpa_s
->global
->p2p
)
1496 p2p_group_formation_failed(wpa_s
->global
->p2p
);
1497 wpas_group_formation_completed(wpa_s
, 0);
1501 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant
*wpa_s
)
1503 wpa_printf(MSG_DEBUG
, "P2P: Reject group formation due to WPS provisioning failure");
1504 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
1505 wpa_s
->parent
, NULL
);
1506 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
1507 wpa_s
->parent
, NULL
);
1508 wpa_s
->global
->p2p_fail_on_wps_complete
= 0;
1512 void wpas_p2p_ap_setup_failed(struct wpa_supplicant
*wpa_s
)
1514 if (wpa_s
->global
->p2p_group_formation
!= wpa_s
)
1516 /* Speed up group formation timeout since this cannot succeed */
1517 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
1518 wpa_s
->parent
, NULL
);
1519 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
1520 wpa_s
->parent
, NULL
);
1524 static void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
1526 struct wpa_supplicant
*wpa_s
= ctx
;
1528 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1529 wpa_drv_cancel_remain_on_channel(wpa_s
);
1530 wpa_s
->off_channel_freq
= 0;
1531 wpa_s
->roc_waiting_drv_freq
= 0;
1535 wpa_msg_global(wpa_s
, MSG_INFO
,
1536 P2P_EVENT_GO_NEG_FAILURE
"status=%d",
1538 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
1539 wpas_p2p_remove_pending_group_interface(wpa_s
);
1543 if (wpa_s
->p2p_go_ht40
)
1545 if (wpa_s
->p2p_go_vht
)
1548 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
"role=%s "
1549 "freq=%d ht40=%d peer_dev=" MACSTR
" peer_iface=" MACSTR
1551 res
->role_go
? "GO" : "client", res
->freq
, res
->ht40
,
1552 MAC2STR(res
->peer_device_addr
),
1553 MAC2STR(res
->peer_interface_addr
),
1554 p2p_wps_method_text(res
->wps_method
));
1555 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
1557 if (res
->role_go
&& wpa_s
->p2p_persistent_id
>= 0) {
1558 struct wpa_ssid
*ssid
;
1559 ssid
= wpa_config_get_network(wpa_s
->conf
,
1560 wpa_s
->p2p_persistent_id
);
1561 if (ssid
&& ssid
->disabled
== 2 &&
1562 ssid
->mode
== WPAS_MODE_P2P_GO
&& ssid
->passphrase
) {
1563 size_t len
= os_strlen(ssid
->passphrase
);
1564 wpa_printf(MSG_DEBUG
, "P2P: Override passphrase based "
1565 "on requested persistent group");
1566 os_memcpy(res
->passphrase
, ssid
->passphrase
, len
);
1567 res
->passphrase
[len
] = '\0';
1571 if (wpa_s
->create_p2p_iface
) {
1572 struct wpa_supplicant
*group_wpa_s
=
1573 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
1574 if (group_wpa_s
== NULL
) {
1575 wpas_p2p_remove_pending_group_interface(wpa_s
);
1578 if (group_wpa_s
!= wpa_s
) {
1579 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
1580 sizeof(group_wpa_s
->p2p_pin
));
1581 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
1583 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1584 wpa_s
->pending_interface_name
[0] = '\0';
1585 group_wpa_s
->p2p_in_provisioning
= 1;
1588 wpas_start_wps_go(group_wpa_s
, res
, 1);
1590 wpas_start_wps_enrollee(group_wpa_s
, res
);
1592 wpa_s
->p2p_in_provisioning
= 1;
1593 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1596 wpas_start_wps_go(wpa_s
, res
, 1);
1598 wpas_start_wps_enrollee(ctx
, res
);
1601 wpa_s
->p2p_long_listen
= 0;
1602 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1604 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1605 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1606 (res
->peer_config_timeout
% 100) * 10000,
1607 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1611 static void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1613 struct wpa_supplicant
*wpa_s
= ctx
;
1614 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1615 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1617 wpas_notify_p2p_go_neg_req(wpa_s
, src
, dev_passwd_id
);
1621 static void wpas_dev_found(void *ctx
, const u8
*addr
,
1622 const struct p2p_peer_info
*info
,
1625 #ifndef CONFIG_NO_STDOUT_DEBUG
1626 struct wpa_supplicant
*wpa_s
= ctx
;
1627 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1628 char *wfd_dev_info_hex
= NULL
;
1630 #ifdef CONFIG_WIFI_DISPLAY
1631 wfd_dev_info_hex
= wifi_display_subelem_hex(info
->wfd_subelems
,
1632 WFD_SUBELEM_DEVICE_INFO
);
1633 #endif /* CONFIG_WIFI_DISPLAY */
1635 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1636 " p2p_dev_addr=" MACSTR
1637 " pri_dev_type=%s name='%s' config_methods=0x%x "
1638 "dev_capab=0x%x group_capab=0x%x%s%s",
1639 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
1640 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
1642 info
->device_name
, info
->config_methods
,
1643 info
->dev_capab
, info
->group_capab
,
1644 wfd_dev_info_hex
? " wfd_dev_info=0x" : "",
1645 wfd_dev_info_hex
? wfd_dev_info_hex
: "");
1646 #endif /* CONFIG_NO_STDOUT_DEBUG */
1648 os_free(wfd_dev_info_hex
);
1650 wpas_notify_p2p_device_found(ctx
, info
->p2p_device_addr
, new_device
);
1654 static void wpas_dev_lost(void *ctx
, const u8
*dev_addr
)
1656 struct wpa_supplicant
*wpa_s
= ctx
;
1658 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_LOST
1659 "p2p_dev_addr=" MACSTR
, MAC2STR(dev_addr
));
1661 wpas_notify_p2p_device_lost(wpa_s
, dev_addr
);
1665 static void wpas_find_stopped(void *ctx
)
1667 struct wpa_supplicant
*wpa_s
= ctx
;
1668 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_FIND_STOPPED
);
1672 struct wpas_p2p_listen_work
{
1674 unsigned int duration
;
1675 struct wpabuf
*probe_resp_ie
;
1679 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work
*lwork
)
1683 wpabuf_free(lwork
->probe_resp_ie
);
1688 static void wpas_p2p_listen_work_done(struct wpa_supplicant
*wpa_s
)
1690 struct wpas_p2p_listen_work
*lwork
;
1692 if (!wpa_s
->p2p_listen_work
)
1695 lwork
= wpa_s
->p2p_listen_work
->ctx
;
1696 wpas_p2p_listen_work_free(lwork
);
1697 radio_work_done(wpa_s
->p2p_listen_work
);
1698 wpa_s
->p2p_listen_work
= NULL
;
1702 static void wpas_start_listen_cb(struct wpa_radio_work
*work
, int deinit
)
1704 struct wpa_supplicant
*wpa_s
= work
->wpa_s
;
1705 struct wpas_p2p_listen_work
*lwork
= work
->ctx
;
1708 wpas_p2p_listen_work_free(lwork
);
1712 wpa_s
->p2p_listen_work
= work
;
1714 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, lwork
->probe_resp_ie
, NULL
);
1716 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1717 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1718 "report received Probe Request frames");
1719 wpas_p2p_listen_work_done(wpa_s
);
1723 wpa_s
->pending_listen_freq
= lwork
->freq
;
1724 wpa_s
->pending_listen_duration
= lwork
->duration
;
1726 if (wpa_drv_remain_on_channel(wpa_s
, lwork
->freq
, lwork
->duration
) < 0)
1728 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1729 "to remain on channel (%u MHz) for Listen "
1730 "state", lwork
->freq
);
1731 wpas_p2p_listen_work_done(wpa_s
);
1732 wpa_s
->pending_listen_freq
= 0;
1735 wpa_s
->off_channel_freq
= 0;
1736 wpa_s
->roc_waiting_drv_freq
= lwork
->freq
;
1740 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1741 unsigned int duration
,
1742 const struct wpabuf
*probe_resp_ie
)
1744 struct wpa_supplicant
*wpa_s
= ctx
;
1745 struct wpas_p2p_listen_work
*lwork
;
1747 if (wpa_s
->p2p_listen_work
) {
1748 wpa_printf(MSG_DEBUG
, "P2P: Reject start_listen since p2p_listen_work already exists");
1752 lwork
= os_zalloc(sizeof(*lwork
));
1756 lwork
->duration
= duration
;
1757 if (probe_resp_ie
) {
1758 lwork
->probe_resp_ie
= wpabuf_dup(probe_resp_ie
);
1759 if (lwork
->probe_resp_ie
== NULL
) {
1760 wpas_p2p_listen_work_free(lwork
);
1765 if (radio_add_work(wpa_s
, freq
, "p2p-listen", 0, wpas_start_listen_cb
,
1767 wpas_p2p_listen_work_free(lwork
);
1775 static void wpas_stop_listen(void *ctx
)
1777 struct wpa_supplicant
*wpa_s
= ctx
;
1778 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1779 wpa_drv_cancel_remain_on_channel(wpa_s
);
1780 wpa_s
->off_channel_freq
= 0;
1781 wpa_s
->roc_waiting_drv_freq
= 0;
1783 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
1784 wpa_drv_probe_req_report(wpa_s
, 0);
1785 wpas_p2p_listen_work_done(wpa_s
);
1789 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1791 struct wpa_supplicant
*wpa_s
= ctx
;
1792 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
), 1);
1797 * DNS Header section is used only to calculate compression pointers, so the
1798 * contents of this data does not matter, but the length needs to be reserved
1799 * in the virtual packet.
1801 #define DNS_HEADER_LEN 12
1804 * 27-octet in-memory packet from P2P specification containing two implied
1805 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1807 #define P2P_SD_IN_MEMORY_LEN 27
1809 static int p2p_sd_dns_uncompress_label(char **upos
, char *uend
, u8
*start
,
1810 u8
**spos
, const u8
*end
)
1812 while (*spos
< end
) {
1813 u8 val
= ((*spos
)[0] & 0xc0) >> 6;
1816 if (val
== 1 || val
== 2) {
1817 /* These are reserved values in RFC 1035 */
1818 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
1819 "sequence starting with 0x%x", val
);
1828 if (*spos
+ 2 > end
) {
1829 wpa_printf(MSG_DEBUG
, "P2P: No room for full "
1830 "DNS offset field");
1834 offset
= (((*spos
)[0] & 0x3f) << 8) | (*spos
)[1];
1835 if (offset
>= *spos
- start
) {
1836 wpa_printf(MSG_DEBUG
, "P2P: Invalid DNS "
1837 "pointer offset %u", offset
);
1842 spos_tmp
= start
+ offset
;
1843 return p2p_sd_dns_uncompress_label(upos
, uend
, start
,
1849 len
= (*spos
)[0] & 0x3f;
1854 if (*spos
+ len
> end
) {
1855 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
1856 "sequence - no room for label with length "
1861 if (*upos
+ len
+ 2 > uend
)
1864 os_memcpy(*upos
, *spos
, len
);
1876 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1877 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1878 * not large enough */
1879 static int p2p_sd_dns_uncompress(char *buf
, size_t buf_len
, const u8
*msg
,
1880 size_t msg_len
, size_t offset
)
1882 /* 27-octet in-memory packet from P2P specification */
1883 const char *prefix
= "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1884 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1885 u8
*tmp
, *end
, *spos
;
1891 if (offset
> msg_len
)
1894 tmp
= os_malloc(DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
+ msg_len
);
1897 spos
= tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
;
1898 end
= spos
+ msg_len
;
1901 os_memset(tmp
, 0, DNS_HEADER_LEN
);
1902 os_memcpy(tmp
+ DNS_HEADER_LEN
, prefix
, P2P_SD_IN_MEMORY_LEN
);
1903 os_memcpy(tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
, msg
, msg_len
);
1906 uend
= buf
+ buf_len
;
1908 ret
= p2p_sd_dns_uncompress_label(&upos
, uend
, tmp
, &spos
, end
);
1917 } else if (upos
[-1] == '.')
1925 static struct p2p_srv_bonjour
*
1926 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1927 const struct wpabuf
*query
)
1929 struct p2p_srv_bonjour
*bsrv
;
1932 len
= wpabuf_len(query
);
1933 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1934 struct p2p_srv_bonjour
, list
) {
1935 if (len
== wpabuf_len(bsrv
->query
) &&
1936 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1944 static struct p2p_srv_upnp
*
1945 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1946 const char *service
)
1948 struct p2p_srv_upnp
*usrv
;
1950 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1951 struct p2p_srv_upnp
, list
) {
1952 if (version
== usrv
->version
&&
1953 os_strcmp(service
, usrv
->service
) == 0)
1960 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1965 if (wpabuf_tailroom(resp
) < 5)
1968 /* Length (to be filled) */
1969 len_pos
= wpabuf_put(resp
, 2);
1970 wpabuf_put_u8(resp
, srv_proto
);
1971 wpabuf_put_u8(resp
, srv_trans_id
);
1973 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1974 /* Response Data: empty */
1975 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1979 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1980 struct wpabuf
*resp
, u8 srv_trans_id
)
1982 struct p2p_srv_bonjour
*bsrv
;
1985 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1987 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1988 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1992 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1993 struct p2p_srv_bonjour
, list
) {
1994 if (wpabuf_tailroom(resp
) <
1995 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1997 /* Length (to be filled) */
1998 len_pos
= wpabuf_put(resp
, 2);
1999 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
2000 wpabuf_put_u8(resp
, srv_trans_id
);
2002 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
2003 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
2004 wpabuf_head(bsrv
->resp
),
2005 wpabuf_len(bsrv
->resp
));
2007 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
2008 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
2009 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
2015 static int match_bonjour_query(struct p2p_srv_bonjour
*bsrv
, const u8
*query
,
2018 char str_rx
[256], str_srv
[256];
2020 if (query_len
< 3 || wpabuf_len(bsrv
->query
) < 3)
2021 return 0; /* Too short to include DNS Type and Version */
2022 if (os_memcmp(query
+ query_len
- 3,
2023 wpabuf_head_u8(bsrv
->query
) + wpabuf_len(bsrv
->query
) - 3,
2025 return 0; /* Mismatch in DNS Type or Version */
2026 if (query_len
== wpabuf_len(bsrv
->query
) &&
2027 os_memcmp(query
, wpabuf_head(bsrv
->query
), query_len
- 3) == 0)
2028 return 1; /* Binary match */
2030 if (p2p_sd_dns_uncompress(str_rx
, sizeof(str_rx
), query
, query_len
- 3,
2032 return 0; /* Failed to uncompress query */
2033 if (p2p_sd_dns_uncompress(str_srv
, sizeof(str_srv
),
2034 wpabuf_head(bsrv
->query
),
2035 wpabuf_len(bsrv
->query
) - 3, 0))
2036 return 0; /* Failed to uncompress service */
2038 return os_strcmp(str_rx
, str_srv
) == 0;
2042 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
2043 struct wpabuf
*resp
, u8 srv_trans_id
,
2044 const u8
*query
, size_t query_len
)
2046 struct p2p_srv_bonjour
*bsrv
;
2050 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
2052 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
2053 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
2054 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
2059 if (query_len
== 0) {
2060 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
2064 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
2065 struct p2p_srv_bonjour
, list
) {
2066 if (!match_bonjour_query(bsrv
, query
, query_len
))
2069 if (wpabuf_tailroom(resp
) <
2070 5 + query_len
+ wpabuf_len(bsrv
->resp
))
2075 /* Length (to be filled) */
2076 len_pos
= wpabuf_put(resp
, 2);
2077 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
2078 wpabuf_put_u8(resp
, srv_trans_id
);
2081 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
2082 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
2083 wpabuf_head(bsrv
->resp
),
2084 wpabuf_len(bsrv
->resp
));
2087 wpabuf_put_data(resp
, query
, query_len
); /* Key */
2088 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
2090 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
2094 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
2096 if (wpabuf_tailroom(resp
) < 5)
2099 /* Length (to be filled) */
2100 len_pos
= wpabuf_put(resp
, 2);
2101 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
2102 wpabuf_put_u8(resp
, srv_trans_id
);
2105 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
2106 /* Response Data: empty */
2107 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
2113 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
2114 struct wpabuf
*resp
, u8 srv_trans_id
)
2116 struct p2p_srv_upnp
*usrv
;
2119 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
2121 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
2122 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
2126 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
2127 struct p2p_srv_upnp
, list
) {
2128 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
2131 /* Length (to be filled) */
2132 len_pos
= wpabuf_put(resp
, 2);
2133 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
2134 wpabuf_put_u8(resp
, srv_trans_id
);
2137 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
2139 wpabuf_put_u8(resp
, usrv
->version
);
2140 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
2142 wpabuf_put_str(resp
, usrv
->service
);
2143 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
2149 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
2150 struct wpabuf
*resp
, u8 srv_trans_id
,
2151 const u8
*query
, size_t query_len
)
2153 struct p2p_srv_upnp
*usrv
;
2159 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
2162 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
2163 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
2164 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
2169 if (query_len
== 0) {
2170 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
2174 if (wpabuf_tailroom(resp
) < 5)
2177 /* Length (to be filled) */
2178 len_pos
= wpabuf_put(resp
, 2);
2179 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
2180 wpabuf_put_u8(resp
, srv_trans_id
);
2183 str
= os_malloc(query_len
);
2186 os_memcpy(str
, query
+ 1, query_len
- 1);
2187 str
[query_len
- 1] = '\0';
2189 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
2190 struct p2p_srv_upnp
, list
) {
2191 if (version
!= usrv
->version
)
2194 if (os_strcmp(str
, "ssdp:all") != 0 &&
2195 os_strstr(usrv
->service
, str
) == NULL
)
2198 if (wpabuf_tailroom(resp
) < 2)
2202 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
2204 wpabuf_put_u8(resp
, version
);
2206 wpabuf_put_u8(resp
, ',');
2210 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
2212 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
2214 wpabuf_put_str(resp
, usrv
->service
);
2219 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
2222 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
2223 /* Response Data: empty */
2226 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
2230 #ifdef CONFIG_WIFI_DISPLAY
2231 static void wpas_sd_req_wfd(struct wpa_supplicant
*wpa_s
,
2232 struct wpabuf
*resp
, u8 srv_trans_id
,
2233 const u8
*query
, size_t query_len
)
2239 wpa_hexdump(MSG_DEBUG
, "P2P: SD Request for WFD", query
, query_len
);
2241 if (!wpa_s
->global
->wifi_display
) {
2242 wpa_printf(MSG_DEBUG
, "P2P: WFD protocol not available");
2243 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_WIFI_DISPLAY
,
2248 if (query_len
< 1) {
2249 wpa_printf(MSG_DEBUG
, "P2P: Missing WFD Requested Device "
2254 if (wpabuf_tailroom(resp
) < 5)
2259 wpa_printf(MSG_DEBUG
, "P2P: WSD for device role 0x%x", role
);
2261 /* TODO: role specific handling */
2263 /* Length (to be filled) */
2264 len_pos
= wpabuf_put(resp
, 2);
2265 wpabuf_put_u8(resp
, P2P_SERV_WIFI_DISPLAY
);
2266 wpabuf_put_u8(resp
, srv_trans_id
);
2267 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
); /* Status Code */
2269 while (pos
< query
+ query_len
) {
2270 if (*pos
< MAX_WFD_SUBELEMS
&&
2271 wpa_s
->global
->wfd_subelem
[*pos
] &&
2272 wpabuf_tailroom(resp
) >=
2273 wpabuf_len(wpa_s
->global
->wfd_subelem
[*pos
])) {
2274 wpa_printf(MSG_DEBUG
, "P2P: Add WSD response "
2275 "subelement %u", *pos
);
2276 wpabuf_put_buf(resp
, wpa_s
->global
->wfd_subelem
[*pos
]);
2281 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
2283 #endif /* CONFIG_WIFI_DISPLAY */
2286 static void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
2287 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
2289 struct wpa_supplicant
*wpa_s
= ctx
;
2290 const u8
*pos
= tlvs
;
2291 const u8
*end
= tlvs
+ tlvs_len
;
2294 struct wpabuf
*resp
;
2295 u8 srv_proto
, srv_trans_id
;
2299 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
2301 buf_len
= 2 * tlvs_len
+ 1;
2302 buf
= os_malloc(buf_len
);
2304 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
2305 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
2307 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
2312 if (wpa_s
->p2p_sd_over_ctrl_iface
) {
2313 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
2314 update_indic
, tlvs
, tlvs_len
);
2315 return; /* to be processed by an external program */
2318 resp
= wpabuf_alloc(10000);
2322 while (pos
+ 1 < end
) {
2323 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
2324 slen
= WPA_GET_LE16(pos
);
2326 if (pos
+ slen
> end
|| slen
< 2) {
2327 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
2332 tlv_end
= pos
+ slen
;
2335 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
2337 srv_trans_id
= *pos
++;
2338 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
2341 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
2342 pos
, tlv_end
- pos
);
2345 if (wpa_s
->force_long_sd
) {
2346 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
2348 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
2349 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
2353 switch (srv_proto
) {
2354 case P2P_SERV_ALL_SERVICES
:
2355 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
2356 "for all services");
2357 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
2358 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
2359 wpa_printf(MSG_DEBUG
, "P2P: No service "
2360 "discovery protocols available");
2361 wpas_sd_add_proto_not_avail(
2362 resp
, P2P_SERV_ALL_SERVICES
,
2366 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
2367 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
2369 case P2P_SERV_BONJOUR
:
2370 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
2371 pos
, tlv_end
- pos
);
2374 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
2375 pos
, tlv_end
- pos
);
2377 #ifdef CONFIG_WIFI_DISPLAY
2378 case P2P_SERV_WIFI_DISPLAY
:
2379 wpas_sd_req_wfd(wpa_s
, resp
, srv_trans_id
,
2380 pos
, tlv_end
- pos
);
2382 #endif /* CONFIG_WIFI_DISPLAY */
2384 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
2385 "protocol %u", srv_proto
);
2386 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
2395 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
2396 update_indic
, tlvs
, tlvs_len
);
2398 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
2404 static void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
2405 const u8
*tlvs
, size_t tlvs_len
)
2407 struct wpa_supplicant
*wpa_s
= ctx
;
2408 const u8
*pos
= tlvs
;
2409 const u8
*end
= tlvs
+ tlvs_len
;
2415 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
2417 if (tlvs_len
> 1500) {
2418 /* TODO: better way for handling this */
2419 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
2420 P2P_EVENT_SERV_DISC_RESP MACSTR
2421 " %u <long response: %u bytes>",
2422 MAC2STR(sa
), update_indic
,
2423 (unsigned int) tlvs_len
);
2425 buf_len
= 2 * tlvs_len
+ 1;
2426 buf
= os_malloc(buf_len
);
2428 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
2429 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
2430 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
2431 MAC2STR(sa
), update_indic
, buf
);
2437 u8 srv_proto
, srv_trans_id
, status
;
2439 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
2440 slen
= WPA_GET_LE16(pos
);
2442 if (pos
+ slen
> end
|| slen
< 3) {
2443 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
2447 tlv_end
= pos
+ slen
;
2450 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
2452 srv_trans_id
= *pos
++;
2453 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
2456 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
2459 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
2460 pos
, tlv_end
- pos
);
2465 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
2469 u64
wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2470 const struct wpabuf
*tlvs
)
2472 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2474 return (uintptr_t) p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
2478 u64
wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2479 u8 version
, const char *query
)
2481 struct wpabuf
*tlvs
;
2484 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
2487 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
2488 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
2489 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
2490 wpabuf_put_u8(tlvs
, version
);
2491 wpabuf_put_str(tlvs
, query
);
2492 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
2498 #ifdef CONFIG_WIFI_DISPLAY
2500 static u64
wpas_p2p_sd_request_wfd(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2501 const struct wpabuf
*tlvs
)
2503 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2505 return (uintptr_t) p2p_sd_request_wfd(wpa_s
->global
->p2p
, dst
, tlvs
);
2509 #define MAX_WFD_SD_SUBELEMS 20
2511 static void wfd_add_sd_req_role(struct wpabuf
*tlvs
, u8 id
, u8 role
,
2512 const char *subelems
)
2519 len
= wpabuf_put(tlvs
, 2);
2520 wpabuf_put_u8(tlvs
, P2P_SERV_WIFI_DISPLAY
); /* Service Protocol Type */
2521 wpabuf_put_u8(tlvs
, id
); /* Service Transaction ID */
2523 wpabuf_put_u8(tlvs
, role
);
2528 if (val
>= 0 && val
< 256) {
2529 wpabuf_put_u8(tlvs
, val
);
2531 if (count
== MAX_WFD_SD_SUBELEMS
)
2534 pos
= os_strchr(pos
+ 1, ',');
2540 WPA_PUT_LE16(len
, (u8
*) wpabuf_put(tlvs
, 0) - len
- 2);
2544 u64
wpas_p2p_sd_request_wifi_display(struct wpa_supplicant
*wpa_s
,
2545 const u8
*dst
, const char *role
)
2547 struct wpabuf
*tlvs
;
2549 const char *subelems
;
2552 subelems
= os_strchr(role
, ' ');
2553 if (subelems
== NULL
)
2557 tlvs
= wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS
));
2561 if (os_strstr(role
, "[source]"))
2562 wfd_add_sd_req_role(tlvs
, id
++, 0x00, subelems
);
2563 if (os_strstr(role
, "[pri-sink]"))
2564 wfd_add_sd_req_role(tlvs
, id
++, 0x01, subelems
);
2565 if (os_strstr(role
, "[sec-sink]"))
2566 wfd_add_sd_req_role(tlvs
, id
++, 0x02, subelems
);
2567 if (os_strstr(role
, "[source+sink]"))
2568 wfd_add_sd_req_role(tlvs
, id
++, 0x03, subelems
);
2570 ret
= wpas_p2p_sd_request_wfd(wpa_s
, dst
, tlvs
);
2575 #endif /* CONFIG_WIFI_DISPLAY */
2578 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, u64 req
)
2580 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2582 return p2p_sd_cancel_request(wpa_s
->global
->p2p
,
2583 (void *) (uintptr_t) req
);
2587 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
2588 const u8
*dst
, u8 dialog_token
,
2589 const struct wpabuf
*resp_tlvs
)
2591 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2593 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
2598 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
2600 if (wpa_s
->global
->p2p
)
2601 p2p_sd_service_update(wpa_s
->global
->p2p
);
2605 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
2607 dl_list_del(&bsrv
->list
);
2608 wpabuf_free(bsrv
->query
);
2609 wpabuf_free(bsrv
->resp
);
2614 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
2616 dl_list_del(&usrv
->list
);
2617 os_free(usrv
->service
);
2622 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
2624 struct p2p_srv_bonjour
*bsrv
, *bn
;
2625 struct p2p_srv_upnp
*usrv
, *un
;
2627 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
2628 struct p2p_srv_bonjour
, list
)
2629 wpas_p2p_srv_bonjour_free(bsrv
);
2631 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
2632 struct p2p_srv_upnp
, list
)
2633 wpas_p2p_srv_upnp_free(usrv
);
2635 wpas_p2p_sd_service_update(wpa_s
);
2639 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
2640 struct wpabuf
*query
, struct wpabuf
*resp
)
2642 struct p2p_srv_bonjour
*bsrv
;
2644 bsrv
= os_zalloc(sizeof(*bsrv
));
2647 bsrv
->query
= query
;
2649 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
2651 wpas_p2p_sd_service_update(wpa_s
);
2656 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
2657 const struct wpabuf
*query
)
2659 struct p2p_srv_bonjour
*bsrv
;
2661 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
2664 wpas_p2p_srv_bonjour_free(bsrv
);
2665 wpas_p2p_sd_service_update(wpa_s
);
2670 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2671 const char *service
)
2673 struct p2p_srv_upnp
*usrv
;
2675 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
2676 return 0; /* Already listed */
2677 usrv
= os_zalloc(sizeof(*usrv
));
2680 usrv
->version
= version
;
2681 usrv
->service
= os_strdup(service
);
2682 if (usrv
->service
== NULL
) {
2686 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
2688 wpas_p2p_sd_service_update(wpa_s
);
2693 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2694 const char *service
)
2696 struct p2p_srv_upnp
*usrv
;
2698 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
2701 wpas_p2p_srv_upnp_free(usrv
);
2702 wpas_p2p_sd_service_update(wpa_s
);
2707 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
2708 const u8
*peer
, const char *params
,
2709 unsigned int generated_pin
)
2711 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2712 " %08d%s", MAC2STR(peer
), generated_pin
, params
);
2716 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
2717 const u8
*peer
, const char *params
)
2719 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2720 "%s", MAC2STR(peer
), params
);
2724 static void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
2725 const u8
*dev_addr
, const u8
*pri_dev_type
,
2726 const char *dev_name
, u16 supp_config_methods
,
2727 u8 dev_capab
, u8 group_capab
, const u8
*group_id
,
2728 size_t group_id_len
)
2730 struct wpa_supplicant
*wpa_s
= ctx
;
2731 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
2733 u8 empty_dev_type
[8];
2734 unsigned int generated_pin
= 0;
2735 struct wpa_supplicant
*group
= NULL
;
2738 for (group
= wpa_s
->global
->ifaces
; group
; group
= group
->next
)
2740 struct wpa_ssid
*s
= group
->current_ssid
;
2742 s
->mode
== WPAS_MODE_P2P_GO
&&
2743 group_id_len
- ETH_ALEN
== s
->ssid_len
&&
2744 os_memcmp(group_id
+ ETH_ALEN
, s
->ssid
,
2750 if (pri_dev_type
== NULL
) {
2751 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
2752 pri_dev_type
= empty_dev_type
;
2754 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
2755 " pri_dev_type=%s name='%s' config_methods=0x%x "
2756 "dev_capab=0x%x group_capab=0x%x%s%s",
2758 wps_dev_type_bin2str(pri_dev_type
, devtype
,
2760 dev_name
, supp_config_methods
, dev_capab
, group_capab
,
2761 group
? " group=" : "",
2762 group
? group
->ifname
: "");
2763 params
[sizeof(params
) - 1] = '\0';
2765 if (config_methods
& WPS_CONFIG_DISPLAY
) {
2766 generated_pin
= wps_generate_pin();
2767 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
2769 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
2770 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
2771 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
2772 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ
2773 MACSTR
"%s", MAC2STR(peer
), params
);
2775 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 1 /* request */,
2776 P2P_PROV_DISC_SUCCESS
,
2777 config_methods
, generated_pin
);
2781 static void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
2783 struct wpa_supplicant
*wpa_s
= ctx
;
2784 unsigned int generated_pin
= 0;
2787 if (wpa_s
->pending_pd_before_join
&&
2788 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
2789 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
2790 wpa_s
->pending_pd_before_join
= 0;
2791 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
2792 "join-existing-group operation");
2793 wpas_p2p_join_start(wpa_s
);
2797 if (wpa_s
->pending_pd_use
== AUTO_PD_JOIN
||
2798 wpa_s
->pending_pd_use
== AUTO_PD_GO_NEG
)
2799 os_snprintf(params
, sizeof(params
), " peer_go=%d",
2800 wpa_s
->pending_pd_use
== AUTO_PD_JOIN
);
2804 if (config_methods
& WPS_CONFIG_DISPLAY
)
2805 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
2806 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
2807 generated_pin
= wps_generate_pin();
2808 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
2810 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
2811 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP
2812 MACSTR
"%s", MAC2STR(peer
), params
);
2814 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
2815 P2P_PROV_DISC_SUCCESS
,
2816 config_methods
, generated_pin
);
2820 static void wpas_prov_disc_fail(void *ctx
, const u8
*peer
,
2821 enum p2p_prov_disc_status status
)
2823 struct wpa_supplicant
*wpa_s
= ctx
;
2825 if (wpa_s
->p2p_fallback_to_go_neg
) {
2826 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: PD for p2p_connect-auto "
2827 "failed - fall back to GO Negotiation");
2828 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
2832 if (status
== P2P_PROV_DISC_TIMEOUT_JOIN
) {
2833 wpa_s
->pending_pd_before_join
= 0;
2834 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
2835 "join-existing-group operation (no ACK for PD "
2837 wpas_p2p_join_start(wpa_s
);
2841 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
2842 " p2p_dev_addr=" MACSTR
" status=%d",
2843 MAC2STR(peer
), status
);
2845 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
2850 static int freq_included(const struct p2p_channels
*channels
, unsigned int freq
)
2852 if (channels
== NULL
)
2853 return 1; /* Assume no restrictions */
2854 return p2p_channels_includes_freq(channels
, freq
);
2859 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
2860 const u8
*go_dev_addr
, const u8
*ssid
,
2861 size_t ssid_len
, int *go
, u8
*group_bssid
,
2862 int *force_freq
, int persistent_group
,
2863 const struct p2p_channels
*channels
)
2865 struct wpa_supplicant
*wpa_s
= ctx
;
2868 struct wpa_supplicant
*grp
;
2870 if (!persistent_group
) {
2871 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
2872 " to join an active group (SSID: %s)",
2873 MAC2STR(sa
), wpa_ssid_txt(ssid
, ssid_len
));
2874 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
2875 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
2877 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
2878 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
2879 "authorized invitation");
2883 * Do not accept the invitation automatically; notify user and
2886 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
2889 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
2891 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
2892 "running persistent group");
2894 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
2898 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
2899 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0) {
2900 wpa_printf(MSG_DEBUG
, "P2P: Accept previously initiated "
2901 "invitation to re-invoke a persistent group");
2902 } else if (!wpa_s
->conf
->persistent_reconnect
)
2903 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
2905 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2906 if (s
->disabled
== 2 &&
2907 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
2908 s
->ssid_len
== ssid_len
&&
2909 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2914 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
2915 " requested reinvocation of an unknown group",
2917 return P2P_SC_FAIL_UNKNOWN_GROUP
;
2920 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
2922 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
2923 wpa_printf(MSG_DEBUG
, "P2P: The only available "
2924 "interface is already in use - reject "
2926 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
2928 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
2929 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
2931 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
2933 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2934 "interface address for the group");
2935 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
2937 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
2942 wpas_p2p_set_own_freq_preference(wpa_s
, 0);
2944 /* Get one of the frequencies currently in use */
2945 if (wpas_p2p_valid_oper_freqs(wpa_s
, &res
, 1) > 0) {
2946 wpa_printf(MSG_DEBUG
, "P2P: Trying to prefer a channel already used by one of the interfaces");
2947 wpas_p2p_set_own_freq_preference(wpa_s
, res
);
2949 if (wpa_s
->num_multichan_concurrent
< 2 ||
2950 wpas_p2p_num_unused_channels(wpa_s
) < 1) {
2951 wpa_printf(MSG_DEBUG
, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2956 if (*force_freq
> 0 && wpa_s
->num_multichan_concurrent
> 1 &&
2957 wpas_p2p_num_unused_channels(wpa_s
) > 0) {
2959 /* We are the client */
2960 wpa_printf(MSG_DEBUG
, "P2P: Peer was found to be "
2961 "running a GO but we are capable of MCC, "
2962 "figure out the best channel to use");
2964 } else if (!freq_included(channels
, *force_freq
)) {
2965 /* We are the GO, and *force_freq is not in the
2967 wpa_printf(MSG_DEBUG
, "P2P: Forced GO freq %d MHz not "
2968 "in intersection but we are capable of MCC, "
2969 "figure out the best channel to use",
2975 return P2P_SC_SUCCESS
;
2979 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
2980 const u8
*ssid
, size_t ssid_len
,
2981 const u8
*go_dev_addr
, u8 status
,
2984 struct wpa_supplicant
*wpa_s
= ctx
;
2987 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2988 if (s
->disabled
== 2 &&
2989 s
->ssid_len
== ssid_len
&&
2990 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2994 if (status
== P2P_SC_SUCCESS
) {
2995 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2996 " was accepted; op_freq=%d MHz, SSID=%s",
2997 MAC2STR(sa
), op_freq
, wpa_ssid_txt(ssid
, ssid_len
));
2999 int go
= s
->mode
== WPAS_MODE_P2P_GO
;
3000 wpas_p2p_group_add_persistent(
3001 wpa_s
, s
, go
, go
? op_freq
: 0, 0, 0, NULL
,
3002 go
? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
: 0);
3004 wpa_s
->user_initiated_pd
= 0;
3005 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
3006 wpa_s
->p2p_wps_method
, 0,
3012 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
3013 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
3014 " was rejected (status %u)", MAC2STR(sa
), status
);
3020 wpa_msg_global(wpa_s
, MSG_INFO
,
3021 P2P_EVENT_INVITATION_RECEIVED
3022 "sa=" MACSTR
" go_dev_addr=" MACSTR
3023 " bssid=" MACSTR
" unknown-network",
3024 MAC2STR(sa
), MAC2STR(go_dev_addr
),
3027 wpa_msg_global(wpa_s
, MSG_INFO
,
3028 P2P_EVENT_INVITATION_RECEIVED
3029 "sa=" MACSTR
" go_dev_addr=" MACSTR
3031 MAC2STR(sa
), MAC2STR(go_dev_addr
));
3036 if (s
->mode
== WPAS_MODE_P2P_GO
&& op_freq
) {
3037 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
3038 "sa=" MACSTR
" persistent=%d freq=%d",
3039 MAC2STR(sa
), s
->id
, op_freq
);
3041 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
3042 "sa=" MACSTR
" persistent=%d",
3043 MAC2STR(sa
), s
->id
);
3048 static void wpas_remove_persistent_peer(struct wpa_supplicant
*wpa_s
,
3049 struct wpa_ssid
*ssid
,
3050 const u8
*peer
, int inv
)
3057 for (i
= 0; ssid
->p2p_client_list
&& i
< ssid
->num_p2p_clients
; i
++) {
3058 if (os_memcmp(ssid
->p2p_client_list
+ i
* ETH_ALEN
, peer
,
3062 if (i
>= ssid
->num_p2p_clients
) {
3063 if (ssid
->mode
!= WPAS_MODE_P2P_GO
&&
3064 os_memcmp(ssid
->bssid
, peer
, ETH_ALEN
) == 0) {
3065 wpa_printf(MSG_DEBUG
, "P2P: Remove persistent group %d "
3066 "due to invitation result", ssid
->id
);
3067 wpas_notify_network_removed(wpa_s
, ssid
);
3068 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
3071 return; /* Peer not found in client list */
3074 wpa_printf(MSG_DEBUG
, "P2P: Remove peer " MACSTR
" from persistent "
3075 "group %d client list%s",
3076 MAC2STR(peer
), ssid
->id
,
3077 inv
? " due to invitation result" : "");
3078 os_memmove(ssid
->p2p_client_list
+ i
* ETH_ALEN
,
3079 ssid
->p2p_client_list
+ (i
+ 1) * ETH_ALEN
,
3080 (ssid
->num_p2p_clients
- i
- 1) * ETH_ALEN
);
3081 ssid
->num_p2p_clients
--;
3082 #ifndef CONFIG_NO_CONFIG_WRITE
3083 if (wpa_s
->parent
->conf
->update_config
&&
3084 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
3085 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
3086 #endif /* CONFIG_NO_CONFIG_WRITE */
3090 static void wpas_remove_persistent_client(struct wpa_supplicant
*wpa_s
,
3093 struct wpa_ssid
*ssid
;
3095 wpa_s
= wpa_s
->global
->p2p_invite_group
;
3097 return; /* No known invitation group */
3098 ssid
= wpa_s
->current_ssid
;
3099 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
3100 !ssid
->p2p_persistent_group
)
3101 return; /* Not operating as a GO in persistent group */
3102 ssid
= wpas_p2p_get_persistent(wpa_s
->parent
, peer
,
3103 ssid
->ssid
, ssid
->ssid_len
);
3104 wpas_remove_persistent_peer(wpa_s
, ssid
, peer
, 1);
3108 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
,
3109 const struct p2p_channels
*channels
,
3110 const u8
*peer
, int neg_freq
)
3112 struct wpa_supplicant
*wpa_s
= ctx
;
3113 struct wpa_ssid
*ssid
;
3117 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
3118 "status=%d " MACSTR
,
3119 status
, MAC2STR(bssid
));
3121 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
3122 "status=%d ", status
);
3124 wpas_notify_p2p_invitation_result(wpa_s
, status
, bssid
);
3126 wpa_printf(MSG_DEBUG
, "P2P: Invitation result - status=%d peer=" MACSTR
,
3127 status
, MAC2STR(peer
));
3128 if (wpa_s
->pending_invite_ssid_id
== -1) {
3129 if (status
== P2P_SC_FAIL_UNKNOWN_GROUP
)
3130 wpas_remove_persistent_client(wpa_s
, peer
);
3131 return; /* Invitation to active group */
3134 if (status
== P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
3135 wpa_printf(MSG_DEBUG
, "P2P: Waiting for peer to start another "
3136 "invitation exchange to indicate readiness for "
3140 if (status
!= P2P_SC_SUCCESS
) {
3141 if (status
== P2P_SC_FAIL_UNKNOWN_GROUP
) {
3142 ssid
= wpa_config_get_network(
3143 wpa_s
->conf
, wpa_s
->pending_invite_ssid_id
);
3144 wpas_remove_persistent_peer(wpa_s
, ssid
, peer
, 1);
3146 wpas_p2p_remove_pending_group_interface(wpa_s
);
3150 ssid
= wpa_config_get_network(wpa_s
->conf
,
3151 wpa_s
->pending_invite_ssid_id
);
3153 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
3154 "data matching with invitation");
3159 * The peer could have missed our ctrl::ack frame for Invitation
3160 * Response and continue retransmitting the frame. To reduce the
3161 * likelihood of the peer not getting successful TX status for the
3162 * Invitation Response frame, wait a short time here before starting
3163 * the persistent group so that we will remain on the current channel to
3164 * acknowledge any possible retransmission from the peer.
3166 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: 50 ms wait on current channel before "
3167 "starting persistent group");
3170 freq
= wpa_s
->p2p_persistent_go_freq
;
3171 if (neg_freq
> 0 && ssid
->mode
== WPAS_MODE_P2P_GO
&&
3172 freq_included(channels
, neg_freq
)) {
3173 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use frequence %d MHz from invitation for GO mode",
3178 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
3179 ssid
->mode
== WPAS_MODE_P2P_GO
,
3181 wpa_s
->p2p_go_ht40
, wpa_s
->p2p_go_vht
,
3183 ssid
->mode
== WPAS_MODE_P2P_GO
?
3184 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
:
3189 static int wpas_p2p_disallowed_freq(struct wpa_global
*global
,
3192 return freq_range_list_includes(&global
->p2p_disallow_freq
, freq
);
3196 static void wpas_p2p_add_chan(struct p2p_reg_class
*reg
, u8 chan
)
3198 reg
->channel
[reg
->channels
] = chan
;
3203 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
3204 struct p2p_channels
*chan
,
3205 struct p2p_channels
*cli_chan
)
3209 os_memset(cli_chan
, 0, sizeof(*cli_chan
));
3211 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
3214 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3215 chan
->reg_class
[cla
].reg_class
= 81;
3216 chan
->reg_class
[cla
].channels
= 0;
3217 for (i
= 0; i
< 11; i
++) {
3218 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 2412 + i
* 5))
3219 wpas_p2p_add_chan(&chan
->reg_class
[cla
], i
+ 1);
3221 if (chan
->reg_class
[cla
].channels
)
3224 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
3227 /* Operating class 115 - 5 GHz, channels 36-48 */
3228 chan
->reg_class
[cla
].reg_class
= 115;
3229 chan
->reg_class
[cla
].channels
= 0;
3230 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 36 * 5))
3231 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 36);
3232 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 40 * 5))
3233 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 40);
3234 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 44 * 5))
3235 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 44);
3236 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 48 * 5))
3237 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 48);
3238 if (chan
->reg_class
[cla
].channels
)
3241 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
3244 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3245 chan
->reg_class
[cla
].reg_class
= 124;
3246 chan
->reg_class
[cla
].channels
= 0;
3247 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 149 * 5))
3248 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 149);
3249 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 153 * 5))
3250 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 153);
3251 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 156 * 5))
3252 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 157);
3253 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 161 * 5))
3254 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 161);
3255 if (chan
->reg_class
[cla
].channels
)
3258 chan
->reg_classes
= cla
;
3263 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
3265 enum hostapd_hw_mode mode
)
3269 for (i
= 0; i
< num_modes
; i
++) {
3270 if (modes
[i
].mode
== mode
)
3279 NOT_ALLOWED
, PASSIVE_ONLY
, ALLOWED
3282 static int has_channel(struct wpa_global
*global
,
3283 struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
3288 freq
= (mode
->mode
== HOSTAPD_MODE_IEEE80211A
? 5000 : 2407) +
3290 if (wpas_p2p_disallowed_freq(global
, freq
))
3293 for (i
= 0; i
< mode
->num_channels
; i
++) {
3294 if (mode
->channels
[i
].chan
== chan
) {
3296 *flags
= mode
->channels
[i
].flag
;
3297 if (mode
->channels
[i
].flag
&
3298 (HOSTAPD_CHAN_DISABLED
|
3299 HOSTAPD_CHAN_RADAR
))
3301 if (mode
->channels
[i
].flag
&
3302 (HOSTAPD_CHAN_PASSIVE_SCAN
|
3303 HOSTAPD_CHAN_NO_IBSS
))
3304 return PASSIVE_ONLY
;
3313 struct p2p_oper_class_map
{
3314 enum hostapd_hw_mode mode
;
3319 enum { BW20
, BW40PLUS
, BW40MINUS
, BW80
} bw
;
3322 static struct p2p_oper_class_map op_class
[] = {
3323 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
3324 #if 0 /* Do not enable HT40 on 2 GHz for now */
3325 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
3326 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
3328 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
3329 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
3330 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
3331 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
3332 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
3333 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
3336 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3337 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3338 * 80 MHz, but currently use the following definition for simplicity
3339 * (these center frequencies are not actual channels, which makes
3340 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3341 * removing invalid channels.
3343 { HOSTAPD_MODE_IEEE80211A
, 128, 36, 161, 4, BW80
},
3344 { -1, 0, 0, 0, 0, BW20
}
3348 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant
*wpa_s
,
3349 struct hostapd_hw_modes
*mode
,
3352 u8 center_channels
[] = { 42, 58, 106, 122, 138, 155 };
3355 if (mode
->mode
!= HOSTAPD_MODE_IEEE80211A
)
3358 for (i
= 0; i
< ARRAY_SIZE(center_channels
); i
++)
3360 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3361 * so the center channel is 6 channels away from the start/end.
3363 if (channel
>= center_channels
[i
] - 6 &&
3364 channel
<= center_channels
[i
] + 6)
3365 return center_channels
[i
];
3371 static enum chan_allowed
wpas_p2p_verify_80mhz(struct wpa_supplicant
*wpa_s
,
3372 struct hostapd_hw_modes
*mode
,
3377 enum chan_allowed res
, ret
= ALLOWED
;
3379 center_chan
= wpas_p2p_get_center_80mhz(wpa_s
, mode
, channel
);
3382 if (center_chan
>= 58 && center_chan
<= 138)
3383 return NOT_ALLOWED
; /* Do not allow DFS channels for P2P */
3385 /* check all the channels are available */
3386 for (i
= 0; i
< 4; i
++) {
3387 int adj_chan
= center_chan
- 6 + i
* 4;
3389 res
= has_channel(wpa_s
->global
, mode
, adj_chan
, &flags
);
3390 if (res
== NOT_ALLOWED
)
3392 if (res
== PASSIVE_ONLY
)
3395 if (i
== 0 && !(flags
& HOSTAPD_CHAN_VHT_10_70
))
3397 if (i
== 1 && !(flags
& HOSTAPD_CHAN_VHT_30_50
))
3399 if (i
== 2 && !(flags
& HOSTAPD_CHAN_VHT_50_30
))
3401 if (i
== 3 && !(flags
& HOSTAPD_CHAN_VHT_70_10
))
3409 static enum chan_allowed
wpas_p2p_verify_channel(struct wpa_supplicant
*wpa_s
,
3410 struct hostapd_hw_modes
*mode
,
3414 enum chan_allowed res
, res2
;
3416 res2
= res
= has_channel(wpa_s
->global
, mode
, channel
, &flag
);
3417 if (bw
== BW40MINUS
) {
3418 if (!(flag
& HOSTAPD_CHAN_HT40MINUS
))
3420 res2
= has_channel(wpa_s
->global
, mode
, channel
- 4, NULL
);
3421 } else if (bw
== BW40PLUS
) {
3422 if (!(flag
& HOSTAPD_CHAN_HT40PLUS
))
3424 res2
= has_channel(wpa_s
->global
, mode
, channel
+ 4, NULL
);
3425 } else if (bw
== BW80
) {
3426 res2
= wpas_p2p_verify_80mhz(wpa_s
, mode
, channel
, bw
);
3429 if (res
== NOT_ALLOWED
|| res2
== NOT_ALLOWED
)
3431 if (res
== PASSIVE_ONLY
|| res2
== PASSIVE_ONLY
)
3432 return PASSIVE_ONLY
;
3437 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
3438 struct p2p_channels
*chan
,
3439 struct p2p_channels
*cli_chan
)
3441 struct hostapd_hw_modes
*mode
;
3442 int cla
, op
, cli_cla
;
3444 if (wpa_s
->hw
.modes
== NULL
) {
3445 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
3446 "of all supported channels; assume dualband "
3448 return wpas_p2p_default_channels(wpa_s
, chan
, cli_chan
);
3453 for (op
= 0; op_class
[op
].op_class
; op
++) {
3454 struct p2p_oper_class_map
*o
= &op_class
[op
];
3456 struct p2p_reg_class
*reg
= NULL
, *cli_reg
= NULL
;
3458 mode
= get_mode(wpa_s
->hw
.modes
, wpa_s
->hw
.num_modes
, o
->mode
);
3461 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
3462 enum chan_allowed res
;
3463 res
= wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
);
3464 if (res
== ALLOWED
) {
3466 wpa_printf(MSG_DEBUG
, "P2P: Add operating class %u",
3468 reg
= &chan
->reg_class
[cla
];
3470 reg
->reg_class
= o
->op_class
;
3472 reg
->channel
[reg
->channels
] = ch
;
3474 } else if (res
== PASSIVE_ONLY
&&
3475 wpa_s
->conf
->p2p_add_cli_chan
) {
3476 if (cli_reg
== NULL
) {
3477 wpa_printf(MSG_DEBUG
, "P2P: Add operating class %u (client only)",
3479 cli_reg
= &cli_chan
->reg_class
[cli_cla
];
3481 cli_reg
->reg_class
= o
->op_class
;
3483 cli_reg
->channel
[cli_reg
->channels
] = ch
;
3484 cli_reg
->channels
++;
3488 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
3489 reg
->channel
, reg
->channels
);
3492 wpa_hexdump(MSG_DEBUG
, "P2P: Channels (client only)",
3493 cli_reg
->channel
, cli_reg
->channels
);
3497 chan
->reg_classes
= cla
;
3498 cli_chan
->reg_classes
= cli_cla
;
3504 int wpas_p2p_get_ht40_mode(struct wpa_supplicant
*wpa_s
,
3505 struct hostapd_hw_modes
*mode
, u8 channel
)
3508 enum chan_allowed ret
;
3510 for (op
= 0; op_class
[op
].op_class
; op
++) {
3511 struct p2p_oper_class_map
*o
= &op_class
[op
];
3514 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
3515 if (o
->mode
!= HOSTAPD_MODE_IEEE80211A
||
3516 o
->bw
== BW20
|| ch
!= channel
)
3518 ret
= wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
);
3520 return (o
->bw
== BW40MINUS
) ? -1 : 1;
3527 int wpas_p2p_get_vht80_center(struct wpa_supplicant
*wpa_s
,
3528 struct hostapd_hw_modes
*mode
, u8 channel
)
3530 if (!wpas_p2p_verify_channel(wpa_s
, mode
, channel
, BW80
))
3533 return wpas_p2p_get_center_80mhz(wpa_s
, mode
, channel
);
3537 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
3540 struct wpa_supplicant
*wpa_s
= ctx
;
3542 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3543 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
3549 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
3553 static int wpas_go_connected(void *ctx
, const u8
*dev_addr
)
3555 struct wpa_supplicant
*wpa_s
= ctx
;
3557 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3558 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
3561 if (ssid
->mode
!= WPAS_MODE_INFRA
)
3563 if (wpa_s
->wpa_state
!= WPA_COMPLETED
&&
3564 wpa_s
->wpa_state
!= WPA_GROUP_HANDSHAKE
)
3566 if (os_memcmp(wpa_s
->go_dev_addr
, dev_addr
, ETH_ALEN
) == 0)
3574 static void wpas_p2p_debug_print(void *ctx
, int level
, const char *msg
)
3576 struct wpa_supplicant
*wpa_s
= ctx
;
3577 wpa_msg_global(wpa_s
, level
, "P2P: %s", msg
);
3581 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant
*wpa_s
)
3583 struct wpa_interface iface
;
3584 struct wpa_supplicant
*p2pdev_wpa_s
;
3586 char force_name
[100];
3589 os_snprintf(ifname
, sizeof(ifname
), P2P_MGMT_DEVICE_PREFIX
"%s",
3591 force_name
[0] = '\0';
3592 wpa_s
->pending_interface_type
= WPA_IF_P2P_DEVICE
;
3593 ret
= wpa_drv_if_add(wpa_s
, WPA_IF_P2P_DEVICE
, ifname
, NULL
, NULL
,
3594 force_name
, wpa_s
->pending_interface_addr
, NULL
);
3596 wpa_printf(MSG_DEBUG
, "P2P: Failed to create P2P Device interface");
3599 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
3600 sizeof(wpa_s
->pending_interface_name
));
3602 os_memset(&iface
, 0, sizeof(iface
));
3604 iface
.ifname
= wpa_s
->pending_interface_name
;
3605 iface
.driver
= wpa_s
->driver
->name
;
3606 iface
.driver_param
= wpa_s
->conf
->driver_param
;
3607 iface
.confname
= wpa_s
->confname
;
3608 p2pdev_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
3609 if (!p2pdev_wpa_s
) {
3610 wpa_printf(MSG_DEBUG
, "P2P: Failed to add P2P Device interface");
3613 p2pdev_wpa_s
->parent
= wpa_s
;
3615 wpa_s
->pending_interface_name
[0] = '\0';
3620 static void wpas_presence_resp(void *ctx
, const u8
*src
, u8 status
,
3621 const u8
*noa
, size_t noa_len
)
3623 struct wpa_supplicant
*wpa_s
, *intf
= ctx
;
3626 for (wpa_s
= intf
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3627 if (wpa_s
->waiting_presence_resp
)
3631 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No group interface was waiting for presence response");
3634 wpa_s
->waiting_presence_resp
= 0;
3636 wpa_snprintf_hex(hex
, sizeof(hex
), noa
, noa_len
);
3637 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PRESENCE_RESPONSE
"src=" MACSTR
3638 " status=%u noa=%s", MAC2STR(src
), status
, hex
);
3643 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3644 * @global: Pointer to global data from wpa_supplicant_init()
3645 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3646 * Returns: 0 on success, -1 on failure
3648 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
3650 struct p2p_config p2p
;
3654 if (wpa_s
->conf
->p2p_disabled
)
3657 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
3663 os_memset(&p2p
, 0, sizeof(p2p
));
3665 p2p
.debug_print
= wpas_p2p_debug_print
;
3666 p2p
.p2p_scan
= wpas_p2p_scan
;
3667 p2p
.send_action
= wpas_send_action
;
3668 p2p
.send_action_done
= wpas_send_action_done
;
3669 p2p
.go_neg_completed
= wpas_go_neg_completed
;
3670 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
3671 p2p
.dev_found
= wpas_dev_found
;
3672 p2p
.dev_lost
= wpas_dev_lost
;
3673 p2p
.find_stopped
= wpas_find_stopped
;
3674 p2p
.start_listen
= wpas_start_listen
;
3675 p2p
.stop_listen
= wpas_stop_listen
;
3676 p2p
.send_probe_resp
= wpas_send_probe_resp
;
3677 p2p
.sd_request
= wpas_sd_request
;
3678 p2p
.sd_response
= wpas_sd_response
;
3679 p2p
.prov_disc_req
= wpas_prov_disc_req
;
3680 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
3681 p2p
.prov_disc_fail
= wpas_prov_disc_fail
;
3682 p2p
.invitation_process
= wpas_invitation_process
;
3683 p2p
.invitation_received
= wpas_invitation_received
;
3684 p2p
.invitation_result
= wpas_invitation_result
;
3685 p2p
.get_noa
= wpas_get_noa
;
3686 p2p
.go_connected
= wpas_go_connected
;
3687 p2p
.presence_resp
= wpas_presence_resp
;
3689 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
3690 os_memcpy(p2p
.dev_addr
, wpa_s
->global
->p2p_dev_addr
, ETH_ALEN
);
3691 p2p
.dev_name
= wpa_s
->conf
->device_name
;
3692 p2p
.manufacturer
= wpa_s
->conf
->manufacturer
;
3693 p2p
.model_name
= wpa_s
->conf
->model_name
;
3694 p2p
.model_number
= wpa_s
->conf
->model_number
;
3695 p2p
.serial_number
= wpa_s
->conf
->serial_number
;
3697 os_memcpy(p2p
.uuid
, wpa_s
->wps
->uuid
, 16);
3698 p2p
.config_methods
= wpa_s
->wps
->config_methods
;
3701 if (wpa_s
->conf
->p2p_listen_reg_class
&&
3702 wpa_s
->conf
->p2p_listen_channel
) {
3703 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
3704 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
3708 * Pick one of the social channels randomly as the listen
3711 os_get_random((u8
*) &r
, sizeof(r
));
3712 p2p
.channel
= 1 + (r
% 3) * 5;
3714 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
3716 if (wpa_s
->conf
->p2p_oper_reg_class
&&
3717 wpa_s
->conf
->p2p_oper_channel
) {
3718 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
3719 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
3720 p2p
.cfg_op_channel
= 1;
3721 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
3722 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
3725 p2p
.op_reg_class
= 81;
3727 * Use random operation channel from (1, 6, 11) if no other
3728 * preference is indicated.
3730 os_get_random((u8
*) &r
, sizeof(r
));
3731 p2p
.op_channel
= 1 + (r
% 3) * 5;
3732 p2p
.cfg_op_channel
= 0;
3733 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
3734 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
3737 if (wpa_s
->conf
->p2p_pref_chan
&& wpa_s
->conf
->num_p2p_pref_chan
) {
3738 p2p
.pref_chan
= wpa_s
->conf
->p2p_pref_chan
;
3739 p2p
.num_pref_chan
= wpa_s
->conf
->num_p2p_pref_chan
;
3742 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
3743 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
3744 p2p
.country
[2] = 0x04;
3746 os_memcpy(p2p
.country
, "XX\x04", 3);
3748 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
, &p2p
.cli_channels
)) {
3749 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
3754 os_memcpy(p2p
.pri_dev_type
, wpa_s
->conf
->device_type
,
3757 p2p
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
3758 os_memcpy(p2p
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
3759 p2p
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
3761 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
3762 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
3764 p2p
.max_peers
= 100;
3766 if (wpa_s
->conf
->p2p_ssid_postfix
) {
3767 p2p
.ssid_postfix_len
=
3768 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
3769 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
3770 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
3771 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
3772 p2p
.ssid_postfix_len
);
3775 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
3777 p2p
.max_listen
= wpa_s
->max_remain_on_chan
;
3779 global
->p2p
= p2p_init(&p2p
);
3780 if (global
->p2p
== NULL
)
3782 global
->p2p_init_wpa_s
= wpa_s
;
3784 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
3785 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
3787 p2p_add_wps_vendor_extension(
3788 global
->p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
3791 p2p_set_no_go_freq(global
->p2p
, &wpa_s
->conf
->p2p_no_go_freq
);
3798 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3799 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3801 * This function deinitialize per-interface P2P data.
3803 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
3805 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
3806 wpa_drv_probe_req_report(wpa_s
, 0);
3808 if (wpa_s
->go_params
) {
3809 /* Clear any stored provisioning info */
3810 p2p_clear_provisioning_info(
3812 wpa_s
->go_params
->peer_device_addr
);
3815 os_free(wpa_s
->go_params
);
3816 wpa_s
->go_params
= NULL
;
3817 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
3818 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3819 wpa_s
->p2p_long_listen
= 0;
3820 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3821 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3822 wpas_p2p_remove_pending_group_interface(wpa_s
);
3823 eloop_cancel_timeout(wpas_p2p_group_freq_conflict
, wpa_s
, NULL
);
3824 wpas_p2p_listen_work_done(wpa_s
);
3825 if (wpa_s
->p2p_send_action_work
) {
3826 os_free(wpa_s
->p2p_send_action_work
->ctx
);
3827 radio_work_done(wpa_s
->p2p_send_action_work
);
3828 wpa_s
->p2p_send_action_work
= NULL
;
3830 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout
, wpa_s
, NULL
);
3832 /* TODO: remove group interface from the driver if this wpa_s instance
3833 * is on top of a P2P group interface */
3838 * wpas_p2p_deinit_global - Deinitialize global P2P module
3839 * @global: Pointer to global data from wpa_supplicant_init()
3841 * This function deinitializes the global (per device) P2P module.
3843 void wpas_p2p_deinit_global(struct wpa_global
*global
)
3845 struct wpa_supplicant
*wpa_s
, *tmp
;
3847 wpa_s
= global
->ifaces
;
3849 wpas_p2p_service_flush(wpa_s
);
3851 if (global
->p2p
== NULL
)
3854 /* Remove remaining P2P group interfaces */
3855 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
3856 wpa_s
= wpa_s
->next
;
3858 tmp
= global
->ifaces
;
3861 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
3866 /* Disconnect from the P2P group and deinit the interface */
3867 wpas_p2p_disconnect(tmp
);
3871 * Deinit GO data on any possibly remaining interface (if main
3872 * interface is used as GO).
3874 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3875 if (wpa_s
->ap_iface
)
3876 wpas_p2p_group_deinit(wpa_s
);
3879 p2p_deinit(global
->p2p
);
3881 global
->p2p_init_wpa_s
= NULL
;
3885 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
3887 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE
) &&
3888 wpa_s
->conf
->p2p_no_group_iface
)
3889 return 0; /* separate interface disabled per configuration */
3890 if (wpa_s
->drv_flags
&
3891 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
3892 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
3893 return 1; /* P2P group requires a new interface in every case
3895 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
3896 return 0; /* driver does not support concurrent operations */
3897 if (wpa_s
->global
->ifaces
->next
)
3898 return 1; /* more that one interface already in use */
3899 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
3900 return 1; /* this interface is already in use */
3905 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
3906 const u8
*peer_addr
,
3907 enum p2p_wps_method wps_method
,
3908 int go_intent
, const u8
*own_interface_addr
,
3909 unsigned int force_freq
, int persistent_group
,
3910 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
3912 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3913 persistent_group
= 2;
3916 * Increase GO config timeout if HT40 is used since it takes some time
3917 * to scan channels for coex purposes before the BSS can be started.
3919 p2p_set_config_timeout(wpa_s
->global
->p2p
,
3920 wpa_s
->p2p_go_ht40
? 255 : 100, 20);
3922 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
3923 go_intent
, own_interface_addr
, force_freq
,
3924 persistent_group
, ssid
? ssid
->ssid
: NULL
,
3925 ssid
? ssid
->ssid_len
: 0,
3926 wpa_s
->p2p_pd_before_go_neg
, pref_freq
);
3930 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
3931 const u8
*peer_addr
,
3932 enum p2p_wps_method wps_method
,
3933 int go_intent
, const u8
*own_interface_addr
,
3934 unsigned int force_freq
, int persistent_group
,
3935 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
3937 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3938 persistent_group
= 2;
3940 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
3941 go_intent
, own_interface_addr
, force_freq
,
3942 persistent_group
, ssid
? ssid
->ssid
: NULL
,
3943 ssid
? ssid
->ssid_len
: 0, pref_freq
);
3947 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
3949 wpa_s
->p2p_join_scan_count
++;
3950 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
3951 wpa_s
->p2p_join_scan_count
);
3952 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
3953 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
3954 " for join operationg - stop join attempt",
3955 MAC2STR(wpa_s
->pending_join_iface_addr
));
3956 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3957 if (wpa_s
->p2p_auto_pd
) {
3958 wpa_s
->p2p_auto_pd
= 0;
3959 wpa_msg_global(wpa_s
, MSG_INFO
,
3960 P2P_EVENT_PROV_DISC_FAILURE
3961 " p2p_dev_addr=" MACSTR
" status=N/A",
3962 MAC2STR(wpa_s
->pending_join_dev_addr
));
3965 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
3966 P2P_EVENT_GROUP_FORMATION_FAILURE
);
3971 static int wpas_check_freq_conflict(struct wpa_supplicant
*wpa_s
, int freq
)
3973 int *freqs
, res
, num
, i
;
3975 if (wpas_p2p_num_unused_channels(wpa_s
) > 0) {
3976 /* Multiple channels are supported and not all are in use */
3980 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
3984 num
= wpas_p2p_valid_oper_freqs(wpa_s
, freqs
,
3985 wpa_s
->num_multichan_concurrent
);
3991 for (i
= 0; i
< num
; i
++) {
3992 if (freqs
[i
] == freq
) {
3993 wpa_printf(MSG_DEBUG
, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4008 static int wpas_p2p_peer_go(struct wpa_supplicant
*wpa_s
,
4009 const u8
*peer_dev_addr
)
4011 struct wpa_bss
*bss
;
4014 bss
= wpa_bss_get_p2p_dev_addr(wpa_s
, peer_dev_addr
);
4017 if (bss
->last_update_idx
< wpa_s
->bss_update_idx
) {
4018 wpa_printf(MSG_DEBUG
, "P2P: Peer BSS entry not updated in the "
4023 updated
= os_reltime_before(&wpa_s
->p2p_auto_started
,
4025 wpa_printf(MSG_DEBUG
, "P2P: Current BSS entry for peer updated at "
4026 "%ld.%06ld (%supdated in last scan)",
4027 bss
->last_update
.sec
, bss
->last_update
.usec
,
4028 updated
? "": "not ");
4034 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
4035 struct wpa_scan_results
*scan_res
)
4037 struct wpa_bss
*bss
= NULL
;
4039 u8 iface_addr
[ETH_ALEN
];
4041 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
4043 if (wpa_s
->global
->p2p_disabled
)
4046 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for %sjoin",
4047 scan_res
? (int) scan_res
->num
: -1,
4048 wpa_s
->p2p_auto_join
? "auto_" : "");
4051 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
4053 if (wpa_s
->p2p_auto_pd
) {
4054 int join
= wpas_p2p_peer_go(wpa_s
,
4055 wpa_s
->pending_join_dev_addr
);
4057 wpa_s
->auto_pd_scan_retry
< P2P_AUTO_PD_SCAN_ATTEMPTS
) {
4058 wpa_s
->auto_pd_scan_retry
++;
4059 bss
= wpa_bss_get_bssid_latest(
4060 wpa_s
, wpa_s
->pending_join_dev_addr
);
4063 wpa_printf(MSG_DEBUG
, "P2P: Scan retry %d for "
4064 "the peer " MACSTR
" at %d MHz",
4065 wpa_s
->auto_pd_scan_retry
,
4067 pending_join_dev_addr
),
4069 wpas_p2p_join_scan_req(wpa_s
, freq
, NULL
, 0);
4077 wpa_s
->p2p_auto_pd
= 0;
4078 wpa_s
->pending_pd_use
= join
? AUTO_PD_JOIN
: AUTO_PD_GO_NEG
;
4079 wpa_printf(MSG_DEBUG
, "P2P: Auto PD with " MACSTR
" join=%d",
4080 MAC2STR(wpa_s
->pending_join_dev_addr
), join
);
4081 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
4082 wpa_s
->pending_join_dev_addr
,
4083 wpa_s
->pending_pd_config_methods
, join
,
4084 0, wpa_s
->user_initiated_pd
) < 0) {
4085 wpa_s
->p2p_auto_pd
= 0;
4086 wpa_msg_global(wpa_s
, MSG_INFO
,
4087 P2P_EVENT_PROV_DISC_FAILURE
4088 " p2p_dev_addr=" MACSTR
" status=N/A",
4089 MAC2STR(wpa_s
->pending_join_dev_addr
));
4094 if (wpa_s
->p2p_auto_join
) {
4095 int join
= wpas_p2p_peer_go(wpa_s
,
4096 wpa_s
->pending_join_dev_addr
);
4098 wpa_printf(MSG_DEBUG
, "P2P: Peer was not found to be "
4099 "running a GO -> use GO Negotiation");
4100 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
,
4101 wpa_s
->p2p_pin
, wpa_s
->p2p_wps_method
,
4102 wpa_s
->p2p_persistent_group
, 0, 0, 0,
4103 wpa_s
->p2p_go_intent
,
4104 wpa_s
->p2p_connect_freq
,
4105 wpa_s
->p2p_persistent_id
,
4106 wpa_s
->p2p_pd_before_go_neg
,
4112 wpa_printf(MSG_DEBUG
, "P2P: Peer was found running GO%s -> "
4113 "try to join the group", join
? "" :
4116 wpa_s
->p2p_fallback_to_go_neg
= 1;
4119 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
4120 wpa_s
->pending_join_iface_addr
);
4122 p2p_get_interface_addr(wpa_s
->global
->p2p
,
4123 wpa_s
->pending_join_dev_addr
,
4125 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
4127 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
4128 "address for join from " MACSTR
" to " MACSTR
4129 " based on newly discovered P2P peer entry",
4130 MAC2STR(wpa_s
->pending_join_iface_addr
),
4131 MAC2STR(iface_addr
));
4132 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
4135 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
4136 wpa_s
->pending_join_iface_addr
);
4139 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
4140 "from P2P peer table: %d MHz", freq
);
4142 if (wpa_s
->p2p_join_ssid_len
) {
4143 wpa_printf(MSG_DEBUG
, "P2P: Trying to find target GO BSS entry based on BSSID "
4144 MACSTR
" and SSID %s",
4145 MAC2STR(wpa_s
->pending_join_iface_addr
),
4146 wpa_ssid_txt(wpa_s
->p2p_join_ssid
,
4147 wpa_s
->p2p_join_ssid_len
));
4148 bss
= wpa_bss_get(wpa_s
, wpa_s
->pending_join_iface_addr
,
4149 wpa_s
->p2p_join_ssid
,
4150 wpa_s
->p2p_join_ssid_len
);
4153 wpa_printf(MSG_DEBUG
, "P2P: Trying to find target GO BSS entry based on BSSID "
4154 MACSTR
, MAC2STR(wpa_s
->pending_join_iface_addr
));
4155 bss
= wpa_bss_get_bssid_latest(wpa_s
,
4156 wpa_s
->pending_join_iface_addr
);
4160 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
4161 "from BSS table: %d MHz (SSID %s)", freq
,
4162 wpa_ssid_txt(bss
->ssid
, bss
->ssid_len
));
4167 if (wpas_check_freq_conflict(wpa_s
, freq
) > 0) {
4168 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
4169 P2P_EVENT_GROUP_FORMATION_FAILURE
4170 "reason=FREQ_CONFLICT");
4174 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
4175 "prior to joining an existing group (GO " MACSTR
4177 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
4178 wpa_s
->pending_pd_before_join
= 1;
4180 switch (wpa_s
->pending_join_wps_method
) {
4181 case WPS_PIN_DISPLAY
:
4182 method
= WPS_CONFIG_KEYPAD
;
4184 case WPS_PIN_KEYPAD
:
4185 method
= WPS_CONFIG_DISPLAY
;
4188 method
= WPS_CONFIG_PUSHBUTTON
;
4195 if ((p2p_get_provisioning_info(wpa_s
->global
->p2p
,
4196 wpa_s
->pending_join_dev_addr
) ==
4199 * We have already performed provision discovery for
4200 * joining the group. Proceed directly to join
4201 * operation without duplicated provision discovery. */
4202 wpa_printf(MSG_DEBUG
, "P2P: Provision discovery "
4203 "with " MACSTR
" already done - proceed to "
4205 MAC2STR(wpa_s
->pending_join_dev_addr
));
4206 wpa_s
->pending_pd_before_join
= 0;
4210 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
4211 wpa_s
->pending_join_dev_addr
, method
, 1,
4212 freq
, wpa_s
->user_initiated_pd
) < 0) {
4213 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
4214 "Discovery Request before joining an "
4216 wpa_s
->pending_pd_before_join
= 0;
4222 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
4223 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
4224 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
4225 wpas_p2p_check_join_scan_limit(wpa_s
);
4229 /* Start join operation immediately */
4230 wpas_p2p_join_start(wpa_s
);
4234 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
,
4235 const u8
*ssid
, size_t ssid_len
)
4238 struct wpa_driver_scan_params params
;
4239 struct wpabuf
*wps_ie
, *ies
;
4241 int freqs
[2] = { 0, 0 };
4243 os_memset(¶ms
, 0, sizeof(params
));
4245 /* P2P Wildcard SSID */
4246 params
.num_ssids
= 1;
4247 if (ssid
&& ssid_len
) {
4248 params
.ssids
[0].ssid
= ssid
;
4249 params
.ssids
[0].ssid_len
= ssid_len
;
4250 os_memcpy(wpa_s
->p2p_join_ssid
, ssid
, ssid_len
);
4251 wpa_s
->p2p_join_ssid_len
= ssid_len
;
4253 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
4254 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
4255 wpa_s
->p2p_join_ssid_len
= 0;
4258 wpa_s
->wps
->dev
.p2p
= 1;
4259 wps_ie
= wps_build_probe_req_ie(DEV_PW_DEFAULT
, &wpa_s
->wps
->dev
,
4260 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
, 0,
4262 if (wps_ie
== NULL
) {
4263 wpas_p2p_scan_res_join(wpa_s
, NULL
);
4267 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
4268 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
4270 wpabuf_free(wps_ie
);
4271 wpas_p2p_scan_res_join(wpa_s
, NULL
);
4274 wpabuf_put_buf(ies
, wps_ie
);
4275 wpabuf_free(wps_ie
);
4277 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
4279 params
.p2p_probe
= 1;
4280 params
.extra_ies
= wpabuf_head(ies
);
4281 params
.extra_ies_len
= wpabuf_len(ies
);
4286 * If freq is not provided, check the operating freq of the GO
4287 * and use a single channel scan on if possible.
4289 oper_freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
4290 wpa_s
->pending_join_iface_addr
);
4296 params
.freqs
= freqs
;
4300 * Run a scan to update BSS table and start Provision Discovery once
4301 * the new scan results become available.
4303 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
4305 os_get_reltime(&wpa_s
->scan_trigger_time
);
4306 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
4307 wpa_s
->own_scan_requested
= 1;
4313 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
4315 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
4316 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
4317 wpas_p2p_check_join_scan_limit(wpa_s
);
4322 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
4324 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
4325 wpas_p2p_join_scan_req(wpa_s
, 0, NULL
, 0);
4329 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
4330 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
4331 int auto_join
, const u8
*ssid
, size_t ssid_len
)
4333 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
4334 MACSTR
" dev " MACSTR
")%s",
4335 MAC2STR(iface_addr
), MAC2STR(dev_addr
),
4336 auto_join
? " (auto_join)" : "");
4337 if (ssid
&& ssid_len
) {
4338 wpa_printf(MSG_DEBUG
, "P2P: Group SSID specified: %s",
4339 wpa_ssid_txt(ssid
, ssid_len
));
4342 wpa_s
->p2p_auto_pd
= 0;
4343 wpa_s
->p2p_auto_join
= !!auto_join
;
4344 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
4345 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
4346 wpa_s
->pending_join_wps_method
= wps_method
;
4348 /* Make sure we are not running find during connection establishment */
4349 wpas_p2p_stop_find(wpa_s
);
4351 wpa_s
->p2p_join_scan_count
= 0;
4352 wpas_p2p_join_scan_req(wpa_s
, 0, ssid
, ssid_len
);
4357 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
4359 struct wpa_supplicant
*group
;
4360 struct p2p_go_neg_results res
;
4361 struct wpa_bss
*bss
;
4363 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
4366 if (group
!= wpa_s
) {
4367 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
4368 sizeof(group
->p2p_pin
));
4369 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
4372 * Need to mark the current interface for p2p_group_formation
4373 * when a separate group interface is not used. This is needed
4374 * to allow p2p_cancel stop a pending p2p_connect-join.
4375 * wpas_p2p_init_group_interface() addresses this for the case
4376 * where a separate group interface is used.
4378 wpa_s
->global
->p2p_group_formation
= wpa_s
;
4381 group
->p2p_in_provisioning
= 1;
4382 group
->p2p_fallback_to_go_neg
= wpa_s
->p2p_fallback_to_go_neg
;
4384 os_memset(&res
, 0, sizeof(res
));
4385 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
4387 res
.wps_method
= wpa_s
->pending_join_wps_method
;
4388 bss
= wpa_bss_get_bssid_latest(wpa_s
, wpa_s
->pending_join_iface_addr
);
4390 res
.freq
= bss
->freq
;
4391 res
.ssid_len
= bss
->ssid_len
;
4392 os_memcpy(res
.ssid
, bss
->ssid
, bss
->ssid_len
);
4393 wpa_printf(MSG_DEBUG
, "P2P: Join target GO operating frequency "
4394 "from BSS table: %d MHz (SSID %s)", bss
->freq
,
4395 wpa_ssid_txt(bss
->ssid
, bss
->ssid_len
));
4398 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
4399 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel prior to "
4401 wpa_drv_cancel_remain_on_channel(wpa_s
);
4402 wpa_s
->off_channel_freq
= 0;
4403 wpa_s
->roc_waiting_drv_freq
= 0;
4405 wpas_start_wps_enrollee(group
, &res
);
4408 * Allow a longer timeout for join-a-running-group than normal 15
4409 * second group formation timeout since the GO may not have authorized
4410 * our connection yet.
4412 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
4413 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
4420 static int wpas_p2p_setup_freqs(struct wpa_supplicant
*wpa_s
, int freq
,
4421 int *force_freq
, int *pref_freq
, int go
)
4424 unsigned int freq_in_use
= 0, num
, i
;
4426 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
4430 num
= get_shared_radio_freqs(wpa_s
, freqs
,
4431 wpa_s
->num_multichan_concurrent
);
4432 wpa_printf(MSG_DEBUG
,
4433 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4434 freq
, wpa_s
->num_multichan_concurrent
, num
);
4439 ret
= p2p_supported_freq(wpa_s
->global
->p2p
, freq
);
4441 ret
= p2p_supported_freq_cli(wpa_s
->global
->p2p
, freq
);
4443 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
4444 "(%u MHz) is not supported for P2P uses",
4450 for (i
= 0; i
< num
; i
++) {
4451 if (freqs
[i
] == freq
)
4455 if (num
== wpa_s
->num_multichan_concurrent
&& !freq_in_use
) {
4456 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4461 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
4462 "requested channel (%u MHz)", freq
);
4467 for (i
= 0; i
< num
; i
++) {
4468 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freqs
[i
]))
4471 if (*pref_freq
== 0 && num
< wpa_s
->num_multichan_concurrent
) {
4472 wpa_printf(MSG_DEBUG
, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4474 *pref_freq
= freqs
[i
];
4476 wpa_printf(MSG_DEBUG
, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4478 *force_freq
= freqs
[i
];
4484 if (num
< wpa_s
->num_multichan_concurrent
&& num
> 0) {
4485 wpa_printf(MSG_DEBUG
, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4487 } else if (num
< wpa_s
->num_multichan_concurrent
) {
4488 wpa_printf(MSG_DEBUG
, "P2P: No current operating channels - try to use a new channel");
4491 wpa_printf(MSG_DEBUG
, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4506 * wpas_p2p_connect - Request P2P Group Formation to be started
4507 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4508 * @peer_addr: Address of the peer P2P Device
4509 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4510 * @persistent_group: Whether to create a persistent group
4511 * @auto_join: Whether to select join vs. GO Negotiation automatically
4512 * @join: Whether to join an existing group (as a client) instead of starting
4513 * Group Owner negotiation; @peer_addr is BSSID in that case
4514 * @auth: Whether to only authorize the connection instead of doing that and
4515 * initiating Group Owner negotiation
4516 * @go_intent: GO Intent or -1 to use default
4517 * @freq: Frequency for the group or 0 for auto-selection
4518 * @persistent_id: Persistent group credentials to use for forcing GO
4519 * parameters or -1 to generate new values (SSID/passphrase)
4520 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4521 * interoperability workaround when initiating group formation
4522 * @ht40: Start GO with 40 MHz channel width
4523 * @vht: Start GO with VHT support
4524 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4525 * failure, -2 on failure due to channel not currently available,
4526 * -3 if forced channel is not supported
4528 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4529 const char *pin
, enum p2p_wps_method wps_method
,
4530 int persistent_group
, int auto_join
, int join
, int auth
,
4531 int go_intent
, int freq
, int persistent_id
, int pd
,
4534 int force_freq
= 0, pref_freq
= 0;
4536 enum wpa_driver_if_type iftype
;
4538 struct wpa_ssid
*ssid
= NULL
;
4540 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4543 if (persistent_id
>= 0) {
4544 ssid
= wpa_config_get_network(wpa_s
->conf
, persistent_id
);
4545 if (ssid
== NULL
|| ssid
->disabled
!= 2 ||
4546 ssid
->mode
!= WPAS_MODE_P2P_GO
)
4550 os_free(wpa_s
->global
->add_psk
);
4551 wpa_s
->global
->add_psk
= NULL
;
4553 wpa_s
->global
->p2p_fail_on_wps_complete
= 0;
4556 go_intent
= wpa_s
->conf
->p2p_go_intent
;
4559 wpa_s
->p2p_long_listen
= 0;
4561 wpa_s
->p2p_wps_method
= wps_method
;
4562 wpa_s
->p2p_persistent_group
= !!persistent_group
;
4563 wpa_s
->p2p_persistent_id
= persistent_id
;
4564 wpa_s
->p2p_go_intent
= go_intent
;
4565 wpa_s
->p2p_connect_freq
= freq
;
4566 wpa_s
->p2p_fallback_to_go_neg
= 0;
4567 wpa_s
->p2p_pd_before_go_neg
= !!pd
;
4568 wpa_s
->p2p_go_ht40
= !!ht40
;
4569 wpa_s
->p2p_go_vht
= !!vht
;
4572 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
4573 else if (wps_method
== WPS_PIN_DISPLAY
) {
4574 ret
= wps_generate_pin();
4575 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
4577 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
4580 wpa_s
->p2p_pin
[0] = '\0';
4582 if (join
|| auto_join
) {
4583 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
4585 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
4586 "connect a running group from " MACSTR
,
4587 MAC2STR(peer_addr
));
4588 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
4591 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
4592 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
4594 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
4595 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
4599 os_get_reltime(&wpa_s
->p2p_auto_started
);
4600 wpa_printf(MSG_DEBUG
, "P2P: Auto join started at "
4602 wpa_s
->p2p_auto_started
.sec
,
4603 wpa_s
->p2p_auto_started
.usec
);
4605 wpa_s
->user_initiated_pd
= 1;
4606 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
,
4607 auto_join
, NULL
, 0) < 0)
4612 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
4616 wpas_p2p_set_own_freq_preference(wpa_s
,
4617 force_freq
? force_freq
: pref_freq
);
4619 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
4621 if (wpa_s
->create_p2p_iface
) {
4622 /* Prepare to add a new interface for the group */
4623 iftype
= WPA_IF_P2P_GROUP
;
4624 if (go_intent
== 15)
4625 iftype
= WPA_IF_P2P_GO
;
4626 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
4627 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
4628 "interface for the group");
4632 if_addr
= wpa_s
->pending_interface_addr
;
4634 if_addr
= wpa_s
->own_addr
;
4637 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
4639 force_freq
, persistent_group
, ssid
,
4645 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
4646 go_intent
, if_addr
, force_freq
,
4647 persistent_group
, ssid
, pref_freq
) < 0) {
4648 if (wpa_s
->create_p2p_iface
)
4649 wpas_p2p_remove_pending_group_interface(wpa_s
);
4657 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4658 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4659 * @freq: Frequency of the channel in MHz
4660 * @duration: Duration of the stay on the channel in milliseconds
4662 * This callback is called when the driver indicates that it has started the
4663 * requested remain-on-channel duration.
4665 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
4666 unsigned int freq
, unsigned int duration
)
4668 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4670 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
4671 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
4672 wpa_s
->pending_listen_duration
);
4673 wpa_s
->pending_listen_freq
= 0;
4678 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
4679 unsigned int timeout
)
4681 /* Limit maximum Listen state time based on driver limitation. */
4682 if (timeout
> wpa_s
->max_remain_on_chan
)
4683 timeout
= wpa_s
->max_remain_on_chan
;
4685 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
4690 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4691 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4692 * @freq: Frequency of the channel in MHz
4694 * This callback is called when the driver indicates that a remain-on-channel
4695 * operation has been completed, i.e., the duration on the requested channel
4698 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
4701 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
4702 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4703 wpa_s
->p2p_long_listen
, offchannel_pending_action_tx(wpa_s
));
4704 wpas_p2p_listen_work_done(wpa_s
);
4705 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4707 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
4708 return; /* P2P module started a new operation */
4709 if (offchannel_pending_action_tx(wpa_s
))
4711 if (wpa_s
->p2p_long_listen
> 0)
4712 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
4713 if (wpa_s
->p2p_long_listen
> 0) {
4714 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
4715 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
4718 * When listen duration is over, stop listen & update p2p_state
4721 p2p_stop_listen(wpa_s
->global
->p2p
);
4727 * wpas_p2p_group_remove - Remove a P2P group
4728 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4729 * @ifname: Network interface name of the group interface or "*" to remove all
4731 * Returns: 0 on success, -1 on failure
4733 * This function is used to remove a P2P group. This can be used to disconnect
4734 * from a group in which the local end is a P2P Client or to end a P2P Group in
4735 * case the local end is the Group Owner. If a virtual network interface was
4736 * created for this group, that interface will be removed. Otherwise, only the
4737 * configured P2P group network will be removed from the interface.
4739 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
4741 struct wpa_global
*global
= wpa_s
->global
;
4743 if (os_strcmp(ifname
, "*") == 0) {
4744 struct wpa_supplicant
*prev
;
4745 wpa_s
= global
->ifaces
;
4748 wpa_s
= wpa_s
->next
;
4749 if (prev
->p2p_group_interface
!=
4750 NOT_P2P_GROUP_INTERFACE
||
4751 (prev
->current_ssid
&&
4752 prev
->current_ssid
->p2p_group
))
4753 wpas_p2p_disconnect(prev
);
4758 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4759 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
4763 return wpas_p2p_disconnect(wpa_s
);
4767 static int wpas_p2p_select_go_freq(struct wpa_supplicant
*wpa_s
, int freq
)
4772 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
4774 if (wpa_s
->best_24_freq
> 0 &&
4775 p2p_supported_freq_go(wpa_s
->global
->p2p
,
4776 wpa_s
->best_24_freq
)) {
4777 freq
= wpa_s
->best_24_freq
;
4778 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
4779 "channel: %d MHz", freq
);
4781 os_get_random((u8
*) &r
, sizeof(r
));
4782 freq
= 2412 + (r
% 3) * 25;
4783 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
4784 "channel: %d MHz", freq
);
4789 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
4791 if (wpa_s
->best_5_freq
> 0 &&
4792 p2p_supported_freq_go(wpa_s
->global
->p2p
,
4793 wpa_s
->best_5_freq
)) {
4794 freq
= wpa_s
->best_5_freq
;
4795 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
4796 "channel: %d MHz", freq
);
4798 os_get_random((u8
*) &r
, sizeof(r
));
4799 freq
= 5180 + (r
% 4) * 20;
4800 if (!p2p_supported_freq_go(wpa_s
->global
->p2p
, freq
)) {
4801 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
4802 "5 GHz channel for P2P group");
4805 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
4806 "channel: %d MHz", freq
);
4810 if (freq
> 0 && !p2p_supported_freq_go(wpa_s
->global
->p2p
, freq
)) {
4811 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
4812 "(%u MHz) is not supported for P2P uses",
4821 static int wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
4822 struct p2p_go_neg_results
*params
,
4823 int freq
, int ht40
, int vht
,
4824 const struct p2p_channels
*channels
)
4827 unsigned int pref_freq
;
4828 unsigned int num
, i
;
4830 os_memset(params
, 0, sizeof(*params
));
4831 params
->role_go
= 1;
4832 params
->ht40
= ht40
;
4835 if (!freq_included(channels
, freq
)) {
4836 wpa_printf(MSG_DEBUG
, "P2P: Forced GO freq %d MHz not "
4840 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
4841 "frequency %d MHz", freq
);
4842 params
->freq
= freq
;
4843 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
4844 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
4845 wpa_s
->conf
->p2p_oper_channel
<= 11 &&
4846 freq_included(channels
,
4847 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
)) {
4848 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
4849 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
4850 "frequency %d MHz", params
->freq
);
4851 } else if ((wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
4852 wpa_s
->conf
->p2p_oper_reg_class
== 116 ||
4853 wpa_s
->conf
->p2p_oper_reg_class
== 117 ||
4854 wpa_s
->conf
->p2p_oper_reg_class
== 124 ||
4855 wpa_s
->conf
->p2p_oper_reg_class
== 126 ||
4856 wpa_s
->conf
->p2p_oper_reg_class
== 127) &&
4857 freq_included(channels
,
4858 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
)) {
4859 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
4860 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
4861 "frequency %d MHz", params
->freq
);
4862 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
4863 wpa_s
->best_overall_freq
> 0 &&
4864 p2p_supported_freq_go(wpa_s
->global
->p2p
,
4865 wpa_s
->best_overall_freq
) &&
4866 freq_included(channels
, wpa_s
->best_overall_freq
)) {
4867 params
->freq
= wpa_s
->best_overall_freq
;
4868 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
4869 "channel %d MHz", params
->freq
);
4870 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
4871 wpa_s
->best_24_freq
> 0 &&
4872 p2p_supported_freq_go(wpa_s
->global
->p2p
,
4873 wpa_s
->best_24_freq
) &&
4874 freq_included(channels
, wpa_s
->best_24_freq
)) {
4875 params
->freq
= wpa_s
->best_24_freq
;
4876 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
4877 "channel %d MHz", params
->freq
);
4878 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
4879 wpa_s
->best_5_freq
> 0 &&
4880 p2p_supported_freq_go(wpa_s
->global
->p2p
,
4881 wpa_s
->best_5_freq
) &&
4882 freq_included(channels
, wpa_s
->best_5_freq
)) {
4883 params
->freq
= wpa_s
->best_5_freq
;
4884 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
4885 "channel %d MHz", params
->freq
);
4886 } else if ((pref_freq
= p2p_get_pref_freq(wpa_s
->global
->p2p
,
4888 params
->freq
= pref_freq
;
4889 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz from preferred "
4890 "channels", params
->freq
);
4893 for (chan
= 0; chan
< 11; chan
++) {
4894 params
->freq
= 2412 + chan
* 5;
4895 if (!wpas_p2p_disallowed_freq(wpa_s
->global
,
4897 freq_included(channels
, params
->freq
))
4901 wpa_printf(MSG_DEBUG
, "P2P: No 2.4 GHz channel "
4905 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
4906 "known)", params
->freq
);
4909 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
4913 res
= wpas_p2p_valid_oper_freqs(wpa_s
, freqs
,
4914 wpa_s
->num_multichan_concurrent
);
4921 for (i
= 0; i
< num
; i
++) {
4922 if (freq
&& freqs
[i
] == freq
)
4924 if (!freq
&& freq_included(channels
, freqs
[i
])) {
4925 wpa_printf(MSG_DEBUG
, "P2P: Force GO on a channel we are already using (%u MHz)",
4927 params
->freq
= freqs
[i
];
4933 if (wpas_p2p_num_unused_channels(wpa_s
) <= 0) {
4935 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq
);
4937 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on any of the channels we are already using");
4940 } else if (num
== 0) {
4941 wpa_printf(MSG_DEBUG
, "P2P: Use one of the free channels");
4943 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4952 static struct wpa_supplicant
*
4953 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
4956 struct wpa_supplicant
*group_wpa_s
;
4958 if (!wpas_p2p_create_iface(wpa_s
)) {
4959 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use same interface for group "
4961 wpa_s
->p2p_first_connection_timeout
= 0;
4965 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
4966 WPA_IF_P2P_CLIENT
) < 0) {
4967 wpa_msg_global(wpa_s
, MSG_ERROR
,
4968 "P2P: Failed to add group interface");
4971 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
4972 if (group_wpa_s
== NULL
) {
4973 wpa_msg_global(wpa_s
, MSG_ERROR
,
4974 "P2P: Failed to initialize group interface");
4975 wpas_p2p_remove_pending_group_interface(wpa_s
);
4979 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use separate group interface %s",
4980 group_wpa_s
->ifname
);
4981 group_wpa_s
->p2p_first_connection_timeout
= 0;
4987 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4988 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4989 * @persistent_group: Whether to create a persistent group
4990 * @freq: Frequency for the group or 0 to indicate no hardcoding
4991 * @ht40: Start GO with 40 MHz channel width
4992 * @vht: Start GO with VHT support
4993 * Returns: 0 on success, -1 on failure
4995 * This function creates a new P2P group with the local end as the Group Owner,
4996 * i.e., without using Group Owner Negotiation.
4998 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
4999 int freq
, int ht40
, int vht
)
5001 struct p2p_go_neg_results params
;
5003 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5006 os_free(wpa_s
->global
->add_psk
);
5007 wpa_s
->global
->add_psk
= NULL
;
5009 /* Make sure we are not running find during connection establishment */
5010 wpa_printf(MSG_DEBUG
, "P2P: Stop any on-going P2P FIND");
5011 wpas_p2p_stop_find_oper(wpa_s
);
5013 freq
= wpas_p2p_select_go_freq(wpa_s
, freq
);
5017 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
, vht
, NULL
))
5020 !p2p_supported_freq_go(wpa_s
->global
->p2p
, params
.freq
)) {
5021 wpa_printf(MSG_DEBUG
, "P2P: The selected channel for GO "
5022 "(%u MHz) is not supported for P2P uses",
5026 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
5027 params
.persistent_group
= persistent_group
;
5029 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
5032 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
5038 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
5039 struct wpa_ssid
*params
, int addr_allocated
)
5041 struct wpa_ssid
*ssid
;
5043 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
5046 wpa_s
->p2p_last_4way_hs_fail
= NULL
;
5048 wpa_supplicant_ap_deinit(wpa_s
);
5050 ssid
= wpa_config_add_network(wpa_s
->conf
);
5053 wpa_config_set_network_defaults(ssid
);
5054 ssid
->temporary
= 1;
5055 ssid
->proto
= WPA_PROTO_RSN
;
5056 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
5057 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
5058 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
5059 ssid
->ssid
= os_malloc(params
->ssid_len
);
5060 if (ssid
->ssid
== NULL
) {
5061 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
5064 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
5065 ssid
->ssid_len
= params
->ssid_len
;
5066 ssid
->p2p_group
= 1;
5067 ssid
->export_keys
= 1;
5068 if (params
->psk_set
) {
5069 os_memcpy(ssid
->psk
, params
->psk
, 32);
5072 if (params
->passphrase
)
5073 ssid
->passphrase
= os_strdup(params
->passphrase
);
5075 wpa_s
->show_group_started
= 1;
5077 wpa_supplicant_select_network(wpa_s
, ssid
);
5083 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
5084 struct wpa_ssid
*ssid
, int addr_allocated
,
5085 int freq
, int ht40
, int vht
,
5086 const struct p2p_channels
*channels
,
5087 int connection_timeout
)
5089 struct p2p_go_neg_results params
;
5092 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
5095 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
5096 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
5097 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
5102 os_free(wpa_s
->global
->add_psk
);
5103 wpa_s
->global
->add_psk
= NULL
;
5105 /* Make sure we are not running find during connection establishment */
5106 wpas_p2p_stop_find_oper(wpa_s
);
5108 wpa_s
->p2p_fallback_to_go_neg
= 0;
5110 if (ssid
->mode
== WPAS_MODE_INFRA
)
5111 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
5113 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
5116 freq
= wpas_p2p_select_go_freq(wpa_s
, freq
);
5120 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
, vht
, channels
))
5124 params
.psk_set
= ssid
->psk_set
;
5126 os_memcpy(params
.psk
, ssid
->psk
, sizeof(params
.psk
));
5127 if (ssid
->passphrase
) {
5128 if (os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
5129 wpa_printf(MSG_ERROR
, "P2P: Invalid passphrase in "
5130 "persistent group");
5133 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
5134 sizeof(params
.passphrase
));
5136 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
5137 params
.ssid_len
= ssid
->ssid_len
;
5138 params
.persistent_group
= 1;
5140 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
5144 wpa_s
->p2p_first_connection_timeout
= connection_timeout
;
5145 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
5151 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
5152 struct wpabuf
*proberesp_ies
)
5154 struct wpa_supplicant
*wpa_s
= ctx
;
5155 if (wpa_s
->ap_iface
) {
5156 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
5157 if (!(hapd
->conf
->p2p
& P2P_GROUP_OWNER
)) {
5158 wpabuf_free(beacon_ies
);
5159 wpabuf_free(proberesp_ies
);
5163 wpabuf_free(hapd
->p2p_beacon_ie
);
5164 hapd
->p2p_beacon_ie
= beacon_ies
;
5166 wpabuf_free(hapd
->p2p_probe_resp_ie
);
5167 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
5169 wpabuf_free(beacon_ies
);
5170 wpabuf_free(proberesp_ies
);
5172 wpa_supplicant_ap_update_beacon(wpa_s
);
5176 static void wpas_p2p_idle_update(void *ctx
, int idle
)
5178 struct wpa_supplicant
*wpa_s
= ctx
;
5179 if (!wpa_s
->ap_iface
)
5181 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
5183 if (wpa_s
->global
->p2p_fail_on_wps_complete
&&
5184 wpa_s
->p2p_in_provisioning
) {
5185 wpas_p2p_grpform_fail_after_wps(wpa_s
);
5188 wpas_p2p_set_group_idle_timeout(wpa_s
);
5190 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
5194 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
5195 struct wpa_ssid
*ssid
)
5197 struct p2p_group
*group
;
5198 struct p2p_group_config
*cfg
;
5200 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5203 cfg
= os_zalloc(sizeof(*cfg
));
5207 if (ssid
->p2p_persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
5208 cfg
->persistent_group
= 2;
5209 else if (ssid
->p2p_persistent_group
)
5210 cfg
->persistent_group
= 1;
5211 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
5212 if (wpa_s
->max_stations
&&
5213 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
5214 cfg
->max_clients
= wpa_s
->max_stations
;
5216 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
5217 os_memcpy(cfg
->ssid
, ssid
->ssid
, ssid
->ssid_len
);
5218 cfg
->ssid_len
= ssid
->ssid_len
;
5219 cfg
->cb_ctx
= wpa_s
;
5220 cfg
->ie_update
= wpas_p2p_ie_update
;
5221 cfg
->idle_update
= wpas_p2p_idle_update
;
5223 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
5226 if (ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
)
5227 p2p_group_notif_formation_done(group
);
5228 wpa_s
->p2p_group
= group
;
5233 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
5236 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
5238 if (!wpa_s
->p2p_in_provisioning
) {
5239 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
5240 "provisioning not in progress");
5244 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
5245 u8 go_dev_addr
[ETH_ALEN
];
5246 os_memcpy(go_dev_addr
, wpa_s
->bssid
, ETH_ALEN
);
5247 wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
5249 /* Clear any stored provisioning info */
5250 p2p_clear_provisioning_info(wpa_s
->global
->p2p
, go_dev_addr
);
5253 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
5255 wpa_s
->p2p_go_group_formation_completed
= 1;
5256 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
5258 * Use a separate timeout for initial data connection to
5259 * complete to allow the group to be removed automatically if
5260 * something goes wrong in this step before the P2P group idle
5261 * timeout mechanism is taken into use.
5263 wpa_dbg(wpa_s
, MSG_DEBUG
,
5264 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5265 P2P_MAX_INITIAL_CONN_WAIT
);
5266 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT
, 0,
5267 wpas_p2p_group_formation_timeout
,
5268 wpa_s
->parent
, NULL
);
5271 * Use a separate timeout for initial data connection to
5272 * complete to allow the group to be removed automatically if
5273 * the client does not complete data connection successfully.
5275 wpa_dbg(wpa_s
, MSG_DEBUG
,
5276 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5277 P2P_MAX_INITIAL_CONN_WAIT_GO
);
5278 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO
, 0,
5279 wpas_p2p_group_formation_timeout
,
5280 wpa_s
->parent
, NULL
);
5282 * Complete group formation on first successful data connection
5284 wpa_s
->p2p_go_group_formation_completed
= 0;
5286 if (wpa_s
->global
->p2p
)
5287 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
5288 wpas_group_formation_completed(wpa_s
, 1);
5292 void wpas_p2p_wps_failed(struct wpa_supplicant
*wpa_s
,
5293 struct wps_event_fail
*fail
)
5295 if (!wpa_s
->p2p_in_provisioning
) {
5296 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS fail event - P2P "
5297 "provisioning not in progress");
5301 if (wpa_s
->go_params
) {
5302 p2p_clear_provisioning_info(
5304 wpa_s
->go_params
->peer_device_addr
);
5307 wpas_notify_p2p_wps_failed(wpa_s
, fail
);
5309 if (wpa_s
== wpa_s
->global
->p2p_group_formation
) {
5311 * Allow some time for the failed WPS negotiation exchange to
5312 * complete, but remove the group since group formation cannot
5313 * succeed after provisioning failure.
5315 wpa_printf(MSG_DEBUG
, "P2P: WPS step failed during group formation - reject connection from timeout");
5316 wpa_s
->global
->p2p_fail_on_wps_complete
= 1;
5317 eloop_deplete_timeout(0, 50000,
5318 wpas_p2p_group_formation_timeout
,
5319 wpa_s
->parent
, NULL
);
5324 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant
*wpa_s
)
5326 if (!wpa_s
->global
->p2p_fail_on_wps_complete
||
5327 !wpa_s
->p2p_in_provisioning
)
5330 wpas_p2p_grpform_fail_after_wps(wpa_s
);
5336 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
5337 const char *config_method
,
5338 enum wpas_p2p_prov_disc_use use
)
5342 wpa_s
->p2p_fallback_to_go_neg
= 0;
5343 wpa_s
->pending_pd_use
= NORMAL_PD
;
5344 if (os_strncmp(config_method
, "display", 7) == 0)
5345 config_methods
= WPS_CONFIG_DISPLAY
;
5346 else if (os_strncmp(config_method
, "keypad", 6) == 0)
5347 config_methods
= WPS_CONFIG_KEYPAD
;
5348 else if (os_strncmp(config_method
, "pbc", 3) == 0 ||
5349 os_strncmp(config_method
, "pushbutton", 10) == 0)
5350 config_methods
= WPS_CONFIG_PUSHBUTTON
;
5352 wpa_printf(MSG_DEBUG
, "P2P: Unknown config method");
5356 if (use
== WPAS_P2P_PD_AUTO
) {
5357 os_memcpy(wpa_s
->pending_join_dev_addr
, peer_addr
, ETH_ALEN
);
5358 wpa_s
->pending_pd_config_methods
= config_methods
;
5359 wpa_s
->p2p_auto_pd
= 1;
5360 wpa_s
->p2p_auto_join
= 0;
5361 wpa_s
->pending_pd_before_join
= 0;
5362 wpa_s
->auto_pd_scan_retry
= 0;
5363 wpas_p2p_stop_find(wpa_s
);
5364 wpa_s
->p2p_join_scan_count
= 0;
5365 os_get_reltime(&wpa_s
->p2p_auto_started
);
5366 wpa_printf(MSG_DEBUG
, "P2P: Auto PD started at %ld.%06ld",
5367 wpa_s
->p2p_auto_started
.sec
,
5368 wpa_s
->p2p_auto_started
.usec
);
5369 wpas_p2p_join_scan(wpa_s
, NULL
);
5373 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
5376 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
5377 config_methods
, use
== WPAS_P2P_PD_FOR_JOIN
,
5382 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
5385 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
5389 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
5391 if (!offchannel_pending_action_tx(wpa_s
))
5394 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
5395 "operation request");
5396 offchannel_clear_pending_action_tx(wpa_s
);
5400 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
5401 enum p2p_discovery_type type
,
5402 unsigned int num_req_dev_types
, const u8
*req_dev_types
,
5403 const u8
*dev_id
, unsigned int search_delay
)
5405 wpas_p2p_clear_pending_action_tx(wpa_s
);
5406 wpa_s
->p2p_long_listen
= 0;
5408 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
||
5409 wpa_s
->p2p_in_provisioning
)
5412 wpa_supplicant_cancel_sched_scan(wpa_s
);
5414 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
,
5415 num_req_dev_types
, req_dev_types
, dev_id
,
5420 static int wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
)
5422 wpas_p2p_clear_pending_action_tx(wpa_s
);
5423 wpa_s
->p2p_long_listen
= 0;
5424 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
5425 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
5427 if (wpa_s
->global
->p2p
)
5428 p2p_stop_find(wpa_s
->global
->p2p
);
5434 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
5436 if (wpas_p2p_stop_find_oper(wpa_s
) > 0)
5438 wpas_p2p_remove_pending_group_interface(wpa_s
);
5442 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
5444 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
5445 wpa_s
->p2p_long_listen
= 0;
5449 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
5453 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5456 wpa_supplicant_cancel_sched_scan(wpa_s
);
5457 wpas_p2p_clear_pending_action_tx(wpa_s
);
5461 * This is a request for unlimited Listen state. However, at
5462 * least for now, this is mapped to a Listen state for one
5467 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
5468 wpa_s
->p2p_long_listen
= 0;
5471 * Stop previous find/listen operation to avoid trying to request a new
5472 * remain-on-channel operation while the driver is still running the
5475 if (wpa_s
->global
->p2p
)
5476 p2p_stop_find(wpa_s
->global
->p2p
);
5478 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
5479 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
5480 wpa_s
->p2p_long_listen
= timeout
* 1000;
5481 eloop_register_timeout(timeout
, 0,
5482 wpas_p2p_long_listen_timeout
,
5490 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
5491 u8
*buf
, size_t len
, int p2p_group
)
5493 struct wpabuf
*p2p_ie
;
5496 if (wpa_s
->global
->p2p_disabled
)
5498 if (wpa_s
->global
->p2p
== NULL
)
5503 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
5504 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
5506 wpabuf_free(p2p_ie
);
5512 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
5513 const u8
*dst
, const u8
*bssid
,
5514 const u8
*ie
, size_t ie_len
, int ssi_signal
)
5516 if (wpa_s
->global
->p2p_disabled
)
5518 if (wpa_s
->global
->p2p
== NULL
)
5521 switch (p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, dst
, bssid
,
5523 case P2P_PREQ_NOT_P2P
:
5524 wpas_notify_preq(wpa_s
, addr
, dst
, bssid
, ie
, ie_len
,
5527 case P2P_PREQ_MALFORMED
:
5528 case P2P_PREQ_NOT_LISTEN
:
5529 case P2P_PREQ_NOT_PROCESSED
:
5530 default: /* make gcc happy */
5532 case P2P_PREQ_PROCESSED
:
5538 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
5539 const u8
*sa
, const u8
*bssid
,
5540 u8 category
, const u8
*data
, size_t len
, int freq
)
5542 if (wpa_s
->global
->p2p_disabled
)
5544 if (wpa_s
->global
->p2p
== NULL
)
5547 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
5552 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
5554 if (wpa_s
->global
->p2p_disabled
)
5556 if (wpa_s
->global
->p2p
== NULL
)
5559 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
5563 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
5565 p2p_group_deinit(wpa_s
->p2p_group
);
5566 wpa_s
->p2p_group
= NULL
;
5568 wpa_s
->ap_configured_cb
= NULL
;
5569 wpa_s
->ap_configured_cb_ctx
= NULL
;
5570 wpa_s
->ap_configured_cb_data
= NULL
;
5571 wpa_s
->connect_without_scan
= NULL
;
5575 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
5577 wpa_s
->p2p_long_listen
= 0;
5579 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5582 return p2p_reject(wpa_s
->global
->p2p
, addr
);
5586 /* Invite to reinvoke a persistent group */
5587 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
5588 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
, int freq
,
5589 int ht40
, int vht
, int pref_freq
)
5591 enum p2p_invite_role role
;
5595 int no_pref_freq_given
= pref_freq
== 0;
5597 wpa_s
->global
->p2p_invite_group
= NULL
;
5599 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
5601 os_memset(wpa_s
->p2p_auth_invite
, 0, ETH_ALEN
);
5603 wpa_s
->p2p_persistent_go_freq
= freq
;
5604 wpa_s
->p2p_go_ht40
= !!ht40
;
5605 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
5606 role
= P2P_INVITE_ROLE_GO
;
5607 if (peer_addr
== NULL
) {
5608 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
5609 "address in invitation command");
5612 if (wpas_p2p_create_iface(wpa_s
)) {
5613 if (wpas_p2p_add_group_interface(wpa_s
,
5614 WPA_IF_P2P_GO
) < 0) {
5615 wpa_printf(MSG_ERROR
, "P2P: Failed to "
5616 "allocate a new interface for the "
5620 bssid
= wpa_s
->pending_interface_addr
;
5622 bssid
= wpa_s
->own_addr
;
5624 role
= P2P_INVITE_ROLE_CLIENT
;
5625 peer_addr
= ssid
->bssid
;
5627 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
5629 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
5630 role
== P2P_INVITE_ROLE_GO
);
5634 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5637 if (wpa_s
->parent
->conf
->p2p_ignore_shared_freq
&&
5638 no_pref_freq_given
&& pref_freq
> 0 &&
5639 wpa_s
->num_multichan_concurrent
> 1 &&
5640 wpas_p2p_num_unused_channels(wpa_s
) > 0) {
5641 wpa_printf(MSG_DEBUG
, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5646 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
5647 ssid
->ssid
, ssid
->ssid_len
, force_freq
, go_dev_addr
,
5652 /* Invite to join an active group */
5653 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
5654 const u8
*peer_addr
, const u8
*go_dev_addr
)
5656 struct wpa_global
*global
= wpa_s
->global
;
5657 enum p2p_invite_role role
;
5659 struct wpa_ssid
*ssid
;
5661 int freq
= 0, force_freq
= 0, pref_freq
= 0;
5664 wpa_s
->p2p_persistent_go_freq
= 0;
5665 wpa_s
->p2p_go_ht40
= 0;
5666 wpa_s
->p2p_go_vht
= 0;
5668 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
5669 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
5672 if (wpa_s
== NULL
) {
5673 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
5677 ssid
= wpa_s
->current_ssid
;
5679 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
5684 wpa_s
->global
->p2p_invite_group
= wpa_s
;
5685 persistent
= ssid
->p2p_persistent_group
&&
5686 wpas_p2p_get_persistent(wpa_s
->parent
, peer_addr
,
5687 ssid
->ssid
, ssid
->ssid_len
);
5689 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
5690 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
5691 bssid
= wpa_s
->own_addr
;
5692 if (go_dev_addr
== NULL
)
5693 go_dev_addr
= wpa_s
->global
->p2p_dev_addr
;
5694 freq
= ssid
->frequency
;
5696 role
= P2P_INVITE_ROLE_CLIENT
;
5697 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
5698 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
5699 "invite to current group");
5702 bssid
= wpa_s
->bssid
;
5703 if (go_dev_addr
== NULL
&&
5704 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
5705 go_dev_addr
= wpa_s
->go_dev_addr
;
5706 freq
= wpa_s
->current_bss
? wpa_s
->current_bss
->freq
:
5707 (int) wpa_s
->assoc_freq
;
5709 wpa_s
->parent
->pending_invite_ssid_id
= -1;
5711 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5714 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
5715 role
== P2P_INVITE_ROLE_ACTIVE_GO
);
5718 wpas_p2p_set_own_freq_preference(wpa_s
, force_freq
);
5720 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
5721 ssid
->ssid
, ssid
->ssid_len
, force_freq
,
5722 go_dev_addr
, persistent
, pref_freq
);
5726 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
5728 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
5729 const char *ssid_txt
;
5730 u8 go_dev_addr
[ETH_ALEN
];
5731 int network_id
= -1;
5735 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
) {
5736 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5737 wpa_s
->parent
, NULL
);
5740 if (!wpa_s
->show_group_started
|| !ssid
)
5743 wpa_s
->show_group_started
= 0;
5745 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
5746 os_memset(go_dev_addr
, 0, ETH_ALEN
);
5747 if (ssid
->bssid_set
)
5748 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
5749 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
5751 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
5753 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
5754 wpa_s
->global
->p2p_group_formation
= NULL
;
5756 freq
= wpa_s
->current_bss
? wpa_s
->current_bss
->freq
:
5757 (int) wpa_s
->assoc_freq
;
5758 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
5760 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
5761 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
5762 "%s client ssid=\"%s\" freq=%d psk=%s "
5763 "go_dev_addr=" MACSTR
"%s",
5764 wpa_s
->ifname
, ssid_txt
, freq
, psk
,
5765 MAC2STR(go_dev_addr
),
5766 persistent
? " [PERSISTENT]" : "");
5768 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
5769 "%s client ssid=\"%s\" freq=%d "
5770 "passphrase=\"%s\" go_dev_addr=" MACSTR
"%s",
5771 wpa_s
->ifname
, ssid_txt
, freq
,
5772 ssid
->passphrase
? ssid
->passphrase
: "",
5773 MAC2STR(go_dev_addr
),
5774 persistent
? " [PERSISTENT]" : "");
5778 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
5781 network_id
= ssid
->id
;
5782 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 1);
5786 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
5787 u32 interval1
, u32 duration2
, u32 interval2
)
5791 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5794 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
5795 wpa_s
->current_ssid
== NULL
||
5796 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
5799 ret
= p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
5800 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
5801 duration1
, interval1
, duration2
, interval2
);
5803 wpa_s
->waiting_presence_resp
= 1;
5809 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
5810 unsigned int interval
)
5812 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5815 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
5819 static int wpas_p2p_is_client(struct wpa_supplicant
*wpa_s
)
5821 if (wpa_s
->current_ssid
== NULL
) {
5823 * current_ssid can be cleared when P2P client interface gets
5824 * disconnected, so assume this interface was used as P2P
5829 return wpa_s
->current_ssid
->p2p_group
&&
5830 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
;
5834 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
5836 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
5838 if (wpa_s
->conf
->p2p_group_idle
== 0 && !wpas_p2p_is_client(wpa_s
)) {
5839 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
5844 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
5846 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_IDLE_TIMEOUT
);
5850 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
5854 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
5855 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
5857 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
5860 timeout
= wpa_s
->conf
->p2p_group_idle
;
5861 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
&&
5862 (timeout
== 0 || timeout
> P2P_MAX_CLIENT_IDLE
))
5863 timeout
= P2P_MAX_CLIENT_IDLE
;
5869 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
)
5870 timeout
= 0; /* special client mode no-timeout */
5875 if (wpa_s
->p2p_in_provisioning
) {
5877 * Use the normal group formation timeout during the
5878 * provisioning phase to avoid terminating this process too
5879 * early due to group idle timeout.
5881 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
5882 "during provisioning");
5886 if (wpa_s
->show_group_started
) {
5888 * Use the normal group formation timeout between the end of
5889 * the provisioning phase and completion of 4-way handshake to
5890 * avoid terminating this process too early due to group idle
5893 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
5894 "while waiting for initial 4-way handshake to "
5899 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
5901 eloop_register_timeout(timeout
, 0, wpas_p2p_group_idle_timeout
,
5906 /* Returns 1 if the interface was removed */
5907 int wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
5908 u16 reason_code
, const u8
*ie
, size_t ie_len
,
5909 int locally_generated
)
5911 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5914 if (!locally_generated
)
5915 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
5918 if (reason_code
== WLAN_REASON_DEAUTH_LEAVING
&& !locally_generated
&&
5919 wpa_s
->current_ssid
&&
5920 wpa_s
->current_ssid
->p2p_group
&&
5921 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
) {
5922 wpa_printf(MSG_DEBUG
, "P2P: GO indicated that the P2P Group "
5923 "session is ending");
5924 if (wpas_p2p_group_delete(wpa_s
,
5925 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
)
5934 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
5935 u16 reason_code
, const u8
*ie
, size_t ie_len
,
5936 int locally_generated
)
5938 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5941 if (!locally_generated
)
5942 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
5947 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
5949 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
5954 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
5957 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
5958 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
5960 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
)
5961 p2p_set_pri_dev_type(p2p
, wpa_s
->conf
->device_type
);
5964 (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_CONFIG_METHODS
))
5965 p2p_set_config_methods(p2p
, wpa_s
->wps
->config_methods
);
5967 if (wpa_s
->wps
&& (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_UUID
))
5968 p2p_set_uuid(p2p
, wpa_s
->wps
->uuid
);
5970 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_WPS_STRING
) {
5971 p2p_set_manufacturer(p2p
, wpa_s
->conf
->manufacturer
);
5972 p2p_set_model_name(p2p
, wpa_s
->conf
->model_name
);
5973 p2p_set_model_number(p2p
, wpa_s
->conf
->model_number
);
5974 p2p_set_serial_number(p2p
, wpa_s
->conf
->serial_number
);
5977 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
)
5978 p2p_set_sec_dev_types(p2p
,
5979 (void *) wpa_s
->conf
->sec_device_type
,
5980 wpa_s
->conf
->num_sec_device_types
);
5982 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_VENDOR_EXTENSION
) {
5984 p2p_remove_wps_vendor_extensions(p2p
);
5985 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
5986 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
5988 p2p_add_wps_vendor_extension(
5989 p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
5993 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
5994 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
5996 country
[0] = wpa_s
->conf
->country
[0];
5997 country
[1] = wpa_s
->conf
->country
[1];
5999 p2p_set_country(p2p
, country
);
6002 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
6003 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
6004 wpa_s
->conf
->p2p_ssid_postfix
?
6005 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
6009 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
6010 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
6012 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_LISTEN_CHANNEL
) {
6013 u8 reg_class
, channel
;
6016 if (wpa_s
->conf
->p2p_listen_reg_class
&&
6017 wpa_s
->conf
->p2p_listen_channel
) {
6018 reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
6019 channel
= wpa_s
->conf
->p2p_listen_channel
;
6023 * Pick one of the social channels randomly as the
6026 os_get_random((u8
*) &r
, sizeof(r
));
6027 channel
= 1 + (r
% 3) * 5;
6029 ret
= p2p_set_listen_channel(p2p
, reg_class
, channel
);
6031 wpa_printf(MSG_ERROR
, "P2P: Own listen channel update "
6034 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_OPER_CHANNEL
) {
6035 u8 op_reg_class
, op_channel
, cfg_op_channel
;
6038 if (wpa_s
->conf
->p2p_oper_reg_class
&&
6039 wpa_s
->conf
->p2p_oper_channel
) {
6040 op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
6041 op_channel
= wpa_s
->conf
->p2p_oper_channel
;
6046 * Use random operation channel from (1, 6, 11)
6047 *if no other preference is indicated.
6049 os_get_random((u8
*) &r
, sizeof(r
));
6050 op_channel
= 1 + (r
% 3) * 5;
6053 ret
= p2p_set_oper_channel(p2p
, op_reg_class
, op_channel
,
6056 wpa_printf(MSG_ERROR
, "P2P: Own oper channel update "
6060 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_PREF_CHAN
) {
6061 if (p2p_set_pref_chan(p2p
, wpa_s
->conf
->num_p2p_pref_chan
,
6062 wpa_s
->conf
->p2p_pref_chan
) < 0) {
6063 wpa_printf(MSG_ERROR
, "P2P: Preferred channel list "
6067 if (p2p_set_no_go_freq(p2p
, &wpa_s
->conf
->p2p_no_go_freq
) < 0) {
6068 wpa_printf(MSG_ERROR
, "P2P: No GO channel list "
6075 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
6078 if (!wpa_s
->ap_iface
)
6080 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
6085 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
6087 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
6090 wpa_s
->global
->cross_connection
= enabled
;
6091 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
6094 struct wpa_supplicant
*iface
;
6096 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
6098 if (iface
->cross_connect_enabled
== 0)
6101 iface
->cross_connect_enabled
= 0;
6102 iface
->cross_connect_in_use
= 0;
6103 wpa_msg_global(iface
->parent
, MSG_INFO
,
6104 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
6106 iface
->cross_connect_uplink
);
6114 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
6116 struct wpa_supplicant
*iface
;
6118 if (!uplink
->global
->cross_connection
)
6121 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
6122 if (!iface
->cross_connect_enabled
)
6124 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
6127 if (iface
->ap_iface
== NULL
)
6129 if (iface
->cross_connect_in_use
)
6132 iface
->cross_connect_in_use
= 1;
6133 wpa_msg_global(iface
->parent
, MSG_INFO
,
6134 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
6135 iface
->ifname
, iface
->cross_connect_uplink
);
6140 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
6142 struct wpa_supplicant
*iface
;
6144 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
6145 if (!iface
->cross_connect_enabled
)
6147 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
6150 if (!iface
->cross_connect_in_use
)
6153 wpa_msg_global(iface
->parent
, MSG_INFO
,
6154 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
6155 iface
->ifname
, iface
->cross_connect_uplink
);
6156 iface
->cross_connect_in_use
= 0;
6161 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
6163 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
6164 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
6165 wpa_s
->cross_connect_disallowed
)
6166 wpas_p2p_disable_cross_connect(wpa_s
);
6168 wpas_p2p_enable_cross_connect(wpa_s
);
6169 if (!wpa_s
->ap_iface
&&
6170 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
6171 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
6175 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
6177 wpas_p2p_disable_cross_connect(wpa_s
);
6178 if (!wpa_s
->ap_iface
&&
6179 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout
,
6181 wpas_p2p_set_group_idle_timeout(wpa_s
);
6185 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
6187 struct wpa_supplicant
*iface
;
6189 if (!wpa_s
->global
->cross_connection
)
6192 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
6195 if (iface
->drv_flags
&
6196 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
6198 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
6201 wpa_s
->cross_connect_enabled
= 1;
6202 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
6203 sizeof(wpa_s
->cross_connect_uplink
));
6204 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
6205 "%s to %s whenever uplink is available",
6206 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
6208 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
6209 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
6210 iface
->cross_connect_disallowed
||
6211 iface
->wpa_state
!= WPA_COMPLETED
)
6214 wpa_s
->cross_connect_in_use
= 1;
6215 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
6216 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
6217 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
6223 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
6225 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
6226 !wpa_s
->p2p_in_provisioning
)
6227 return 0; /* not P2P client operation */
6229 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
6231 if (wpa_s
!= wpa_s
->parent
)
6232 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
6233 wpas_p2p_group_formation_failed(wpa_s
);
6238 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
6240 struct p2p_channels chan
, cli_chan
;
6242 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
6245 os_memset(&chan
, 0, sizeof(chan
));
6246 os_memset(&cli_chan
, 0, sizeof(cli_chan
));
6247 if (wpas_p2p_setup_channels(wpa_s
, &chan
, &cli_chan
)) {
6248 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
6253 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
, &cli_chan
);
6257 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant
*wpa_s
,
6258 struct wpa_scan_results
*scan_res
)
6260 wpa_printf(MSG_DEBUG
, "P2P: Ignore scan results");
6264 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
6266 struct wpa_global
*global
= wpa_s
->global
;
6270 if (global
->p2p
== NULL
)
6273 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
6275 if (wpa_s
->pending_interface_name
[0] &&
6276 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
6279 peer
= p2p_get_go_neg_peer(global
->p2p
);
6281 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
6282 MACSTR
, MAC2STR(peer
));
6283 p2p_unauthorize(global
->p2p
, peer
);
6287 if (wpa_s
->scan_res_handler
== wpas_p2p_scan_res_join
) {
6288 wpa_printf(MSG_DEBUG
, "P2P: Stop pending scan for join");
6289 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_ignore
;
6293 if (wpa_s
->pending_pd_before_join
) {
6294 wpa_printf(MSG_DEBUG
, "P2P: Stop pending PD before join");
6295 wpa_s
->pending_pd_before_join
= 0;
6299 wpas_p2p_stop_find(wpa_s
);
6301 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
6302 if (wpa_s
== global
->p2p_group_formation
&&
6303 (wpa_s
->p2p_in_provisioning
||
6304 wpa_s
->parent
->pending_interface_type
==
6305 WPA_IF_P2P_CLIENT
)) {
6306 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
6307 "formation found - cancelling",
6310 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
6311 wpa_s
->parent
, NULL
);
6312 if (wpa_s
->p2p_in_provisioning
) {
6313 wpas_group_formation_completed(wpa_s
, 0);
6316 wpas_p2p_group_delete(wpa_s
,
6317 P2P_GROUP_REMOVAL_REQUESTED
);
6323 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
6331 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
6333 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
6336 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
6337 "being available anymore");
6338 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_UNAVAILABLE
);
6342 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
6343 int freq_24
, int freq_5
, int freq_overall
)
6345 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
6348 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
6352 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
6355 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
6360 if (hwaddr_aton(addr
, peer
))
6363 return p2p_unauthorize(p2p
, peer
);
6368 * wpas_p2p_disconnect - Disconnect from a P2P Group
6369 * @wpa_s: Pointer to wpa_supplicant data
6370 * Returns: 0 on success, -1 on failure
6372 * This can be used to disconnect from a group in which the local end is a P2P
6373 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6374 * virtual network interface was created for this group, that interface will be
6375 * removed. Otherwise, only the configured P2P group network will be removed
6376 * from the interface.
6378 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
6384 return wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_REQUESTED
) < 0 ?
6389 int wpas_p2p_in_progress(struct wpa_supplicant
*wpa_s
)
6393 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
6396 ret
= p2p_in_progress(wpa_s
->global
->p2p
);
6399 * Check whether there is an ongoing WPS provisioning step (or
6400 * other parts of group formation) on another interface since
6401 * p2p_in_progress() does not report this to avoid issues for
6402 * scans during such provisioning step.
6404 if (wpa_s
->global
->p2p_group_formation
&&
6405 wpa_s
->global
->p2p_group_formation
!= wpa_s
) {
6406 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Another interface (%s) "
6407 "in group formation",
6408 wpa_s
->global
->p2p_group_formation
->ifname
);
6413 if (!ret
&& wpa_s
->global
->p2p_go_wait_client
.sec
) {
6414 struct os_reltime now
;
6415 os_get_reltime(&now
);
6416 if (os_reltime_expired(&now
, &wpa_s
->global
->p2p_go_wait_client
,
6417 P2P_MAX_INITIAL_CONN_WAIT_GO
)) {
6418 /* Wait for the first client has expired */
6419 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
6421 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Waiting for initial client connection during group formation");
6430 void wpas_p2p_network_removed(struct wpa_supplicant
*wpa_s
,
6431 struct wpa_ssid
*ssid
)
6433 if (wpa_s
->p2p_in_provisioning
&& ssid
->p2p_group
&&
6434 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
6435 wpa_s
->parent
, NULL
) > 0) {
6437 * Remove the network by scheduling the group formation
6438 * timeout to happen immediately. The teardown code
6439 * needs to be scheduled to run asynch later so that we
6440 * don't delete data from under ourselves unexpectedly.
6441 * Calling wpas_p2p_group_formation_timeout directly
6442 * causes a series of crashes in WPS failure scenarios.
6444 wpa_printf(MSG_DEBUG
, "P2P: Canceled group formation due to "
6445 "P2P group network getting removed");
6446 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
6447 wpa_s
->parent
, NULL
);
6452 struct wpa_ssid
* wpas_p2p_get_persistent(struct wpa_supplicant
*wpa_s
,
6453 const u8
*addr
, const u8
*ssid
,
6459 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
6460 if (s
->disabled
!= 2)
6463 (ssid_len
!= s
->ssid_len
||
6464 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0))
6467 if (s
->mode
== WPAS_MODE_P2P_GO
)
6471 if (os_memcmp(s
->bssid
, addr
, ETH_ALEN
) == 0)
6472 return s
; /* peer is GO in the persistent group */
6473 if (s
->mode
!= WPAS_MODE_P2P_GO
|| s
->p2p_client_list
== NULL
)
6475 for (i
= 0; i
< s
->num_p2p_clients
; i
++) {
6476 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
,
6477 addr
, ETH_ALEN
) == 0)
6478 return s
; /* peer is P2P client in persistent
6487 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant
*wpa_s
,
6490 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
6491 wpa_s
->parent
, NULL
) > 0) {
6493 * This can happen if WPS provisioning step is not terminated
6494 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6495 * peer was able to connect, there is no need to time out group
6496 * formation after this, though. In addition, this is used with
6497 * the initial connection wait on the GO as a separate formation
6498 * timeout and as such, expected to be hit after the initial WPS
6499 * provisioning step.
6501 wpa_printf(MSG_DEBUG
, "P2P: Canceled P2P group formation timeout on data connection");
6503 if (!wpa_s
->p2p_go_group_formation_completed
) {
6504 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Marking group formation completed on GO on first data connection");
6505 wpa_s
->p2p_go_group_formation_completed
= 1;
6506 wpa_s
->global
->p2p_group_formation
= NULL
;
6507 wpa_s
->p2p_in_provisioning
= 0;
6509 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
6512 wpas_p2p_add_persistent_group_client(wpa_s
, addr
);
6516 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
6519 struct wpa_supplicant
*group
= wpa_s
;
6520 if (wpa_s
->global
->p2p_group_formation
)
6521 group
= wpa_s
->global
->p2p_group_formation
;
6522 wpa_s
= wpa_s
->parent
;
6523 offchannel_send_action_done(wpa_s
);
6525 wpas_p2p_group_delete(group
, P2P_GROUP_REMOVAL_SILENT
);
6526 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Fall back to GO Negotiation");
6527 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
, wpa_s
->p2p_pin
,
6528 wpa_s
->p2p_wps_method
, wpa_s
->p2p_persistent_group
, 0,
6529 0, 0, wpa_s
->p2p_go_intent
, wpa_s
->p2p_connect_freq
,
6530 wpa_s
->p2p_persistent_id
,
6531 wpa_s
->p2p_pd_before_go_neg
,
6537 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant
*wpa_s
)
6539 if (!wpa_s
->p2p_fallback_to_go_neg
||
6540 wpa_s
->p2p_in_provisioning
<= 5)
6543 if (wpas_p2p_peer_go(wpa_s
, wpa_s
->pending_join_dev_addr
) > 0)
6544 return 0; /* peer operating as a GO */
6546 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: GO not found for p2p_connect-auto - "
6547 "fallback to GO Negotiation");
6548 wpas_p2p_fallback_to_go_neg(wpa_s
, 1);
6554 unsigned int wpas_p2p_search_delay(struct wpa_supplicant
*wpa_s
)
6556 struct wpa_supplicant
*ifs
;
6558 if (wpa_s
->wpa_state
> WPA_SCANNING
) {
6559 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search delay due to "
6560 "concurrent operation",
6561 P2P_CONCURRENT_SEARCH_DELAY
);
6562 return P2P_CONCURRENT_SEARCH_DELAY
;
6565 dl_list_for_each(ifs
, &wpa_s
->radio
->ifaces
, struct wpa_supplicant
,
6567 if (ifs
!= wpa_s
&& ifs
->wpa_state
> WPA_SCANNING
) {
6568 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search "
6569 "delay due to concurrent operation on "
6571 P2P_CONCURRENT_SEARCH_DELAY
, ifs
->ifname
);
6572 return P2P_CONCURRENT_SEARCH_DELAY
;
6580 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant
*wpa_s
,
6581 struct wpa_ssid
*s
, const u8
*addr
,
6584 struct psk_list_entry
*psk
, *tmp
;
6587 dl_list_for_each_safe(psk
, tmp
, &s
->psk_list
, struct psk_list_entry
,
6589 if ((iface_addr
&& !psk
->p2p
&&
6590 os_memcmp(addr
, psk
->addr
, ETH_ALEN
) == 0) ||
6591 (!iface_addr
&& psk
->p2p
&&
6592 os_memcmp(addr
, psk
->addr
, ETH_ALEN
) == 0)) {
6593 wpa_dbg(wpa_s
, MSG_DEBUG
,
6594 "P2P: Remove persistent group PSK list entry for "
6596 MAC2STR(psk
->addr
), psk
->p2p
);
6597 dl_list_del(&psk
->list
);
6607 void wpas_p2p_new_psk_cb(struct wpa_supplicant
*wpa_s
, const u8
*mac_addr
,
6608 const u8
*p2p_dev_addr
,
6609 const u8
*psk
, size_t psk_len
)
6611 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
6612 struct wpa_ssid
*persistent
;
6613 struct psk_list_entry
*p
;
6615 if (psk_len
!= sizeof(p
->psk
))
6619 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: New PSK for addr=" MACSTR
6620 " p2p_dev_addr=" MACSTR
,
6621 MAC2STR(mac_addr
), MAC2STR(p2p_dev_addr
));
6622 if (is_zero_ether_addr(p2p_dev_addr
))
6623 p2p_dev_addr
= NULL
;
6625 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: New PSK for addr=" MACSTR
,
6629 if (ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
6630 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: new_psk_cb during group formation");
6631 /* To be added to persistent group once created */
6632 if (wpa_s
->global
->add_psk
== NULL
) {
6633 wpa_s
->global
->add_psk
= os_zalloc(sizeof(*p
));
6634 if (wpa_s
->global
->add_psk
== NULL
)
6637 p
= wpa_s
->global
->add_psk
;
6640 os_memcpy(p
->addr
, p2p_dev_addr
, ETH_ALEN
);
6643 os_memcpy(p
->addr
, mac_addr
, ETH_ALEN
);
6645 os_memcpy(p
->psk
, psk
, psk_len
);
6649 if (ssid
->mode
!= WPAS_MODE_P2P_GO
|| !ssid
->p2p_persistent_group
) {
6650 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Ignore new_psk_cb on not-persistent GO");
6654 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, NULL
, ssid
->ssid
,
6657 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not find persistent group information to store the new PSK");
6661 p
= os_zalloc(sizeof(*p
));
6666 os_memcpy(p
->addr
, p2p_dev_addr
, ETH_ALEN
);
6669 os_memcpy(p
->addr
, mac_addr
, ETH_ALEN
);
6671 os_memcpy(p
->psk
, psk
, psk_len
);
6673 if (dl_list_len(&persistent
->psk_list
) > P2P_MAX_STORED_CLIENTS
) {
6674 struct psk_list_entry
*last
;
6675 last
= dl_list_last(&persistent
->psk_list
,
6676 struct psk_list_entry
, list
);
6677 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove oldest PSK entry for "
6678 MACSTR
" (p2p=%u) to make room for a new one",
6679 MAC2STR(last
->addr
), last
->p2p
);
6680 dl_list_del(&last
->list
);
6684 wpas_p2p_remove_psk_entry(wpa_s
->parent
, persistent
,
6685 p2p_dev_addr
? p2p_dev_addr
: mac_addr
,
6686 p2p_dev_addr
== NULL
);
6688 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add new PSK for p2p_dev_addr="
6689 MACSTR
, MAC2STR(p2p_dev_addr
));
6691 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add new PSK for addr=" MACSTR
,
6694 dl_list_add(&persistent
->psk_list
, &p
->list
);
6696 #ifndef CONFIG_NO_CONFIG_WRITE
6697 if (wpa_s
->parent
->conf
->update_config
&&
6698 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
6699 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
6700 #endif /* CONFIG_NO_CONFIG_WRITE */
6704 static void wpas_p2p_remove_psk(struct wpa_supplicant
*wpa_s
,
6705 struct wpa_ssid
*s
, const u8
*addr
,
6710 res
= wpas_p2p_remove_psk_entry(wpa_s
, s
, addr
, iface_addr
);
6712 #ifndef CONFIG_NO_CONFIG_WRITE
6713 if (wpa_s
->conf
->update_config
&&
6714 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
))
6715 wpa_dbg(wpa_s
, MSG_DEBUG
,
6716 "P2P: Failed to update configuration");
6717 #endif /* CONFIG_NO_CONFIG_WRITE */
6722 static void wpas_p2p_remove_client_go(struct wpa_supplicant
*wpa_s
,
6723 const u8
*peer
, int iface_addr
)
6725 struct hostapd_data
*hapd
;
6726 struct hostapd_wpa_psk
*psk
, *prev
, *rem
;
6727 struct sta_info
*sta
;
6729 if (wpa_s
->ap_iface
== NULL
|| wpa_s
->current_ssid
== NULL
||
6730 wpa_s
->current_ssid
->mode
!= WPAS_MODE_P2P_GO
)
6733 /* Remove per-station PSK entry */
6734 hapd
= wpa_s
->ap_iface
->bss
[0];
6736 psk
= hapd
->conf
->ssid
.wpa_psk
;
6738 if ((iface_addr
&& os_memcmp(peer
, psk
->addr
, ETH_ALEN
) == 0) ||
6740 os_memcmp(peer
, psk
->p2p_dev_addr
, ETH_ALEN
) == 0)) {
6741 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove operating group PSK entry for "
6742 MACSTR
" iface_addr=%d",
6743 MAC2STR(peer
), iface_addr
);
6745 prev
->next
= psk
->next
;
6747 hapd
->conf
->ssid
.wpa_psk
= psk
->next
;
6757 /* Disconnect from group */
6759 sta
= ap_get_sta(hapd
, peer
);
6761 sta
= ap_get_sta_p2p(hapd
, peer
);
6763 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Disconnect peer " MACSTR
6764 " (iface_addr=%d) from group",
6765 MAC2STR(peer
), iface_addr
);
6766 hostapd_drv_sta_deauth(hapd
, sta
->addr
,
6767 WLAN_REASON_DEAUTH_LEAVING
);
6768 ap_sta_deauthenticate(hapd
, sta
, WLAN_REASON_DEAUTH_LEAVING
);
6773 void wpas_p2p_remove_client(struct wpa_supplicant
*wpa_s
, const u8
*peer
,
6777 struct wpa_supplicant
*w
;
6779 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove client " MACSTR
, MAC2STR(peer
));
6781 /* Remove from any persistent group */
6782 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
6783 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
6786 wpas_remove_persistent_peer(wpa_s
, s
, peer
, 0);
6787 wpas_p2p_remove_psk(wpa_s
->parent
, s
, peer
, iface_addr
);
6790 /* Remove from any operating group */
6791 for (w
= wpa_s
->global
->ifaces
; w
; w
= w
->next
)
6792 wpas_p2p_remove_client_go(w
, peer
, iface_addr
);
6796 static void wpas_p2p_psk_failure_removal(void *eloop_ctx
, void *timeout_ctx
)
6798 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
6799 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_PSK_FAILURE
);
6803 static void wpas_p2p_group_freq_conflict(void *eloop_ctx
, void *timeout_ctx
)
6805 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
6807 wpa_printf(MSG_DEBUG
, "P2P: Frequency conflict - terminate group");
6808 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_FREQ_CONFLICT
);
6812 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant
*wpa_s
, int freq
,
6813 struct wpa_ssid
*ssid
)
6815 struct wpa_supplicant
*iface
;
6817 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
6818 if (!iface
->current_ssid
||
6819 iface
->current_ssid
->frequency
== freq
||
6820 (iface
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
&&
6821 !iface
->current_ssid
->p2p_group
))
6824 /* Remove the connection with least priority */
6825 if (!wpas_is_p2p_prioritized(iface
)) {
6826 /* STA connection has priority over existing
6827 * P2P connection, so remove the interface. */
6828 wpa_printf(MSG_DEBUG
, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6829 eloop_register_timeout(0, 0,
6830 wpas_p2p_group_freq_conflict
,
6832 /* If connection in progress is P2P connection, do not
6833 * proceed for the connection. */
6839 /* P2P connection has priority, disable the STA network
6841 wpa_supplicant_disable_network(wpa_s
->global
->ifaces
,
6843 wpa_msg(wpa_s
->global
->ifaces
, MSG_INFO
,
6844 WPA_EVENT_FREQ_CONFLICT
" id=%d", ssid
->id
);
6845 os_memset(wpa_s
->global
->ifaces
->pending_bssid
, 0,
6847 /* If P2P connection is in progress, continue
6860 int wpas_p2p_4way_hs_failed(struct wpa_supplicant
*wpa_s
)
6862 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
6864 if (ssid
== NULL
|| !ssid
->p2p_group
)
6867 if (wpa_s
->p2p_last_4way_hs_fail
&&
6868 wpa_s
->p2p_last_4way_hs_fail
== ssid
) {
6869 u8 go_dev_addr
[ETH_ALEN
];
6870 struct wpa_ssid
*persistent
;
6872 if (wpas_p2p_persistent_group(wpa_s
, go_dev_addr
,
6874 ssid
->ssid_len
) <= 0) {
6875 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6879 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6880 MACSTR
, MAC2STR(go_dev_addr
));
6881 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, go_dev_addr
,
6884 if (persistent
== NULL
|| persistent
->mode
!= WPAS_MODE_INFRA
) {
6885 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No matching persistent group stored");
6888 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
6889 P2P_EVENT_PERSISTENT_PSK_FAIL
"%d",
6892 wpa_s
->p2p_last_4way_hs_fail
= NULL
;
6894 * Remove the group from a timeout to avoid issues with caller
6895 * continuing to use the interface if this is on a P2P group
6898 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal
,
6903 wpa_s
->p2p_last_4way_hs_fail
= ssid
;