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/wps_hostapd.h"
24 #include "ap/p2p_hostapd.h"
25 #include "eapol_supp/eapol_supp_sm.h"
26 #include "rsn_supp/wpa.h"
27 #include "wpa_supplicant_i.h"
30 #include "config_ssid.h"
35 #include "offchannel.h"
36 #include "wps_supplicant.h"
37 #include "p2p_supplicant.h"
38 #include "wifi_display.h"
42 * How many times to try to scan to find the GO before giving up on join
45 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49 #ifndef P2P_MAX_CLIENT_IDLE
51 * How many seconds to try to reconnect to the GO when connection in P2P client
54 #define P2P_MAX_CLIENT_IDLE 10
55 #endif /* P2P_MAX_CLIENT_IDLE */
57 #ifndef P2P_MAX_INITIAL_CONN_WAIT
59 * How many seconds to wait for initial 4-way handshake to get completed after
60 * WPS provisioning step or after the re-invocation of a persistent group on a
63 #define P2P_MAX_INITIAL_CONN_WAIT 10
64 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
66 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
68 * How many seconds to wait for initial 4-way handshake to get completed after
69 * WPS provisioning step on the GO. This controls the extra time the P2P
70 * operation is considered to be in progress (e.g., to delay other scans) after
71 * WPS provisioning has been completed on the GO during group formation.
73 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
74 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
76 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
78 * How many seconds to wait for initial 4-way handshake to get completed after
79 * re-invocation of a persistent group on the GO when the client is expected
80 * to connect automatically (no user interaction).
82 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
83 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
85 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
87 enum p2p_group_removal_reason
{
88 P2P_GROUP_REMOVAL_UNKNOWN
,
89 P2P_GROUP_REMOVAL_SILENT
,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED
,
91 P2P_GROUP_REMOVAL_REQUESTED
,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT
,
93 P2P_GROUP_REMOVAL_UNAVAILABLE
,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
,
95 P2P_GROUP_REMOVAL_PSK_FAILURE
,
96 P2P_GROUP_REMOVAL_FREQ_CONFLICT
100 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
101 static struct wpa_supplicant
*
102 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
104 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
, int freq
,
105 const u8
*ssid
, size_t ssid_len
);
106 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
,
107 const u8
*ssid
, size_t ssid_len
);
108 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
);
109 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
110 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
111 int auto_join
, int freq
,
112 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 int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
122 static void wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
);
123 static void wpas_stop_listen(void *ctx
);
124 static void wpas_p2p_psk_failure_removal(void *eloop_ctx
, void *timeout_ctx
);
125 static void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
);
126 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
127 enum wpa_driver_if_type type
);
131 * Get the number of concurrent channels that the HW can operate, but that are
132 * currently not in use by any of the wpa_supplicant interfaces.
134 static int wpas_p2p_num_unused_channels(struct wpa_supplicant
*wpa_s
)
139 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
143 num
= get_shared_radio_freqs(wpa_s
, freqs
,
144 wpa_s
->num_multichan_concurrent
);
147 unused
= wpa_s
->num_multichan_concurrent
- num
;
148 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: num_unused_channels: %d", unused
);
154 * Get the frequencies that are currently in use by one or more of the virtual
155 * interfaces, and that are also valid for P2P operation.
158 wpas_p2p_valid_oper_freqs(struct wpa_supplicant
*wpa_s
,
159 struct wpa_used_freq_data
*p2p_freqs
,
162 struct wpa_used_freq_data
*freqs
;
163 unsigned int num
, i
, j
;
165 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
,
166 sizeof(struct wpa_used_freq_data
));
170 num
= get_shared_radio_freqs_data(wpa_s
, freqs
,
171 wpa_s
->num_multichan_concurrent
);
173 os_memset(p2p_freqs
, 0, sizeof(struct wpa_used_freq_data
) * len
);
175 for (i
= 0, j
= 0; i
< num
&& j
< len
; i
++) {
176 if (p2p_supported_freq(wpa_s
->global
->p2p
, freqs
[i
].freq
))
177 p2p_freqs
[j
++] = freqs
[i
];
182 dump_freq_data(wpa_s
, "valid for P2P", p2p_freqs
, j
);
188 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant
*wpa_s
,
191 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
193 if (wpa_s
->parent
->conf
->p2p_ignore_shared_freq
&&
194 freq
> 0 && wpa_s
->num_multichan_concurrent
> 1 &&
195 wpas_p2p_num_unused_channels(wpa_s
) > 0) {
196 wpa_printf(MSG_DEBUG
, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
200 p2p_set_own_freq_preference(wpa_s
->global
->p2p
, freq
);
204 static void wpas_p2p_scan_res_handler(struct wpa_supplicant
*wpa_s
,
205 struct wpa_scan_results
*scan_res
)
209 if (wpa_s
->p2p_scan_work
) {
210 struct wpa_radio_work
*work
= wpa_s
->p2p_scan_work
;
211 wpa_s
->p2p_scan_work
= NULL
;
212 radio_work_done(work
);
215 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
218 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS)",
219 (int) scan_res
->num
);
221 for (i
= 0; i
< scan_res
->num
; i
++) {
222 struct wpa_scan_res
*bss
= scan_res
->res
[i
];
223 struct os_reltime time_tmp_age
, entry_ts
;
227 time_tmp_age
.sec
= bss
->age
/ 1000;
228 time_tmp_age
.usec
= (bss
->age
% 1000) * 1000;
229 os_reltime_sub(&scan_res
->fetch_time
, &time_tmp_age
, &entry_ts
);
231 ies
= (const u8
*) (bss
+ 1);
232 ies_len
= bss
->ie_len
;
233 if (bss
->beacon_ie_len
> 0 &&
234 !wpa_scan_get_vendor_ie(bss
, P2P_IE_VENDOR_TYPE
) &&
235 wpa_scan_get_vendor_ie_beacon(bss
, P2P_IE_VENDOR_TYPE
)) {
236 wpa_printf(MSG_DEBUG
, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
237 MACSTR
, MAC2STR(bss
->bssid
));
239 ies_len
= bss
->beacon_ie_len
;
243 if (p2p_scan_res_handler(wpa_s
->global
->p2p
, bss
->bssid
,
244 bss
->freq
, &entry_ts
, bss
->level
,
249 p2p_scan_res_handled(wpa_s
->global
->p2p
);
253 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work
*work
, int deinit
)
255 struct wpa_supplicant
*wpa_s
= work
->wpa_s
;
256 struct wpa_driver_scan_params
*params
= work
->ctx
;
260 if (!work
->started
) {
261 wpa_scan_free_params(params
);
265 wpa_s
->p2p_scan_work
= NULL
;
269 ret
= wpa_drv_scan(wpa_s
, params
);
270 wpa_scan_free_params(params
);
273 radio_work_done(work
);
274 p2p_notify_scan_trigger_status(wpa_s
->global
->p2p
, ret
);
278 p2p_notify_scan_trigger_status(wpa_s
->global
->p2p
, ret
);
279 os_get_reltime(&wpa_s
->scan_trigger_time
);
280 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
281 wpa_s
->own_scan_requested
= 1;
282 wpa_s
->p2p_scan_work
= work
;
286 static int wpas_p2p_search_social_channel(struct wpa_supplicant
*wpa_s
,
289 if (wpa_s
->global
->p2p_24ghz_social_channels
&&
290 (freq
== 2412 || freq
== 2437 || freq
== 2462)) {
292 * Search all social channels regardless of whether these have
293 * been disabled for P2P operating channel use to avoid missing
298 return p2p_supported_freq(wpa_s
->global
->p2p
, freq
);
302 static int wpas_p2p_scan(void *ctx
, enum p2p_scan_type type
, int freq
,
303 unsigned int num_req_dev_types
,
304 const u8
*req_dev_types
, const u8
*dev_id
, u16 pw_id
)
306 struct wpa_supplicant
*wpa_s
= ctx
;
307 struct wpa_driver_scan_params
*params
= NULL
;
308 struct wpabuf
*wps_ie
, *ies
;
309 unsigned int num_channels
= 0;
310 int social_channels_freq
[] = { 2412, 2437, 2462, 60480 };
314 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
317 if (wpa_s
->p2p_scan_work
) {
318 wpa_dbg(wpa_s
, MSG_INFO
, "P2P: Reject scan trigger since one is already pending");
322 params
= os_zalloc(sizeof(*params
));
326 /* P2P Wildcard SSID */
327 params
->num_ssids
= 1;
328 n
= os_malloc(P2P_WILDCARD_SSID_LEN
);
331 os_memcpy(n
, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
);
332 params
->ssids
[0].ssid
= n
;
333 params
->ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
335 wpa_s
->wps
->dev
.p2p
= 1;
336 wps_ie
= wps_build_probe_req_ie(pw_id
, &wpa_s
->wps
->dev
,
337 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
,
338 num_req_dev_types
, req_dev_types
);
342 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
343 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
348 wpabuf_put_buf(ies
, wps_ie
);
351 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, dev_id
);
353 params
->p2p_probe
= 1;
354 n
= os_malloc(wpabuf_len(ies
));
359 os_memcpy(n
, wpabuf_head(ies
), wpabuf_len(ies
));
360 params
->extra_ies
= n
;
361 params
->extra_ies_len
= wpabuf_len(ies
);
365 case P2P_SCAN_SOCIAL
:
366 params
->freqs
= os_calloc(ARRAY_SIZE(social_channels_freq
) + 1,
368 if (params
->freqs
== NULL
)
370 for (i
= 0; i
< ARRAY_SIZE(social_channels_freq
); i
++) {
371 if (wpas_p2p_search_social_channel(
372 wpa_s
, social_channels_freq
[i
]))
373 params
->freqs
[num_channels
++] =
374 social_channels_freq
[i
];
376 params
->freqs
[num_channels
++] = 0;
380 case P2P_SCAN_SOCIAL_PLUS_ONE
:
381 params
->freqs
= os_calloc(ARRAY_SIZE(social_channels_freq
) + 2,
383 if (params
->freqs
== NULL
)
385 for (i
= 0; i
< ARRAY_SIZE(social_channels_freq
); i
++) {
386 if (wpas_p2p_search_social_channel(
387 wpa_s
, social_channels_freq
[i
]))
388 params
->freqs
[num_channels
++] =
389 social_channels_freq
[i
];
391 if (p2p_supported_freq(wpa_s
->global
->p2p
, freq
))
392 params
->freqs
[num_channels
++] = freq
;
393 params
->freqs
[num_channels
++] = 0;
397 radio_remove_works(wpa_s
, "p2p-scan", 0);
398 if (radio_add_work(wpa_s
, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb
,
404 wpa_scan_free_params(params
);
409 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
411 switch (p2p_group_interface
) {
412 case P2P_GROUP_INTERFACE_PENDING
:
413 return WPA_IF_P2P_GROUP
;
414 case P2P_GROUP_INTERFACE_GO
:
415 return WPA_IF_P2P_GO
;
416 case P2P_GROUP_INTERFACE_CLIENT
:
417 return WPA_IF_P2P_CLIENT
;
420 return WPA_IF_P2P_GROUP
;
424 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
426 size_t ssid_len
, int *go
)
430 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
431 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
432 if (s
->disabled
!= 0 || !s
->p2p_group
||
433 s
->ssid_len
!= ssid_len
||
434 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
436 if (s
->mode
== WPAS_MODE_P2P_GO
&&
437 s
!= wpa_s
->current_ssid
)
440 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
449 static void run_wpas_p2p_disconnect(void *eloop_ctx
, void *timeout_ctx
)
451 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
452 wpa_printf(MSG_DEBUG
,
453 "P2P: Complete previously requested removal of %s",
455 wpas_p2p_disconnect(wpa_s
);
459 static int wpas_p2p_disconnect_safely(struct wpa_supplicant
*wpa_s
,
460 struct wpa_supplicant
*calling_wpa_s
)
462 if (calling_wpa_s
== wpa_s
&& wpa_s
&&
463 wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
465 * The calling wpa_s instance is going to be removed. Do that
466 * from an eloop callback to keep the instance available until
467 * the caller has returned. This my be needed, e.g., to provide
468 * control interface responses on the per-interface socket.
470 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect
,
476 return wpas_p2p_disconnect(wpa_s
);
480 /* Determine total number of clients in active groups where we are the GO */
481 static unsigned int p2p_group_go_member_count(struct wpa_supplicant
*wpa_s
)
483 unsigned int count
= 0;
486 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
487 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
488 wpa_printf(MSG_DEBUG
,
489 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
490 wpa_s
, s
, s
->disabled
, s
->p2p_group
,
492 if (!s
->disabled
&& s
->p2p_group
&&
493 s
->mode
== WPAS_MODE_P2P_GO
) {
494 count
+= p2p_get_group_num_members(
504 /* Find an interface for a P2P group where we are the GO */
505 static struct wpa_supplicant
*
506 wpas_p2p_get_go_group(struct wpa_supplicant
*wpa_s
)
508 struct wpa_supplicant
*save
= NULL
;
514 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
515 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
516 if (s
->disabled
|| !s
->p2p_group
||
517 s
->mode
!= WPAS_MODE_P2P_GO
)
520 /* Prefer a group with connected clients */
521 if (p2p_get_group_num_members(wpa_s
->p2p_group
))
527 /* No group with connected clients, so pick the one without (if any) */
532 /* Find an active P2P group where we are the GO */
533 static struct wpa_ssid
* wpas_p2p_group_go_ssid(struct wpa_supplicant
*wpa_s
,
536 struct wpa_ssid
*s
, *empty
= NULL
;
541 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
542 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
543 if (s
->disabled
|| !s
->p2p_group
||
544 s
->mode
!= WPAS_MODE_P2P_GO
)
547 os_memcpy(bssid
, wpa_s
->own_addr
, ETH_ALEN
);
548 if (p2p_get_group_num_members(wpa_s
->p2p_group
))
558 /* Find a persistent group where we are the GO */
559 static struct wpa_ssid
*
560 wpas_p2p_get_persistent_go(struct wpa_supplicant
*wpa_s
)
564 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
565 if (s
->disabled
== 2 && s
->mode
== WPAS_MODE_P2P_GO
)
573 static u8
p2ps_group_capability(void *ctx
, u8 incoming
, u8 role
)
575 struct wpa_supplicant
*wpa_s
= ctx
, *tmp_wpa_s
;
577 u8 conncap
= P2PS_SETUP_NONE
;
578 unsigned int owned_members
= 0;
579 unsigned int owner
= 0;
580 unsigned int client
= 0;
581 struct wpa_supplicant
*go_wpa_s
;
582 struct wpa_ssid
*persistent_go
;
583 int p2p_no_group_iface
;
585 wpa_printf(MSG_DEBUG
, "P2P: Conncap - in:%d role:%d", incoming
, role
);
588 * For non-concurrent capable devices:
589 * If persistent_go, then no new.
590 * If GO, then no client.
591 * If client, then no GO.
593 go_wpa_s
= wpas_p2p_get_go_group(wpa_s
);
594 persistent_go
= wpas_p2p_get_persistent_go(wpa_s
);
595 p2p_no_group_iface
= wpa_s
->conf
->p2p_no_group_iface
;
597 wpa_printf(MSG_DEBUG
, "P2P: GO(iface)=%p persistent(ssid)=%p",
598 go_wpa_s
, persistent_go
);
600 for (tmp_wpa_s
= wpa_s
->global
->ifaces
; tmp_wpa_s
;
601 tmp_wpa_s
= tmp_wpa_s
->next
) {
602 for (s
= tmp_wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
603 wpa_printf(MSG_DEBUG
,
604 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
605 tmp_wpa_s
, s
, s
->disabled
,
606 s
->p2p_group
, s
->mode
);
607 if (!s
->disabled
&& s
->p2p_group
) {
608 if (s
->mode
== WPAS_MODE_P2P_GO
) {
610 p2p_get_group_num_members(
611 tmp_wpa_s
->p2p_group
);
619 /* If not concurrent, restrict our choices */
620 if (p2p_no_group_iface
) {
621 wpa_printf(MSG_DEBUG
, "P2P: p2p_no_group_iface");
624 return P2PS_SETUP_NONE
;
627 if (role
== P2PS_SETUP_CLIENT
||
628 incoming
== P2PS_SETUP_GROUP_OWNER
||
629 p2p_client_limit_reached(go_wpa_s
->p2p_group
))
630 return P2PS_SETUP_NONE
;
632 return P2PS_SETUP_GROUP_OWNER
;
636 if (role
== P2PS_SETUP_NONE
|| role
== P2PS_SETUP_NEW
) {
638 return P2PS_SETUP_GROUP_OWNER
|
640 if (incoming
== P2PS_SETUP_NEW
) {
643 if (os_get_random(&r
, sizeof(r
)) < 0 ||
645 return P2PS_SETUP_CLIENT
;
646 return P2PS_SETUP_GROUP_OWNER
;
652 /* If a required role has been specified, handle it here */
653 if (role
&& role
!= P2PS_SETUP_NEW
) {
655 case P2PS_SETUP_NONE
:
657 case P2PS_SETUP_GROUP_OWNER
| P2PS_SETUP_CLIENT
:
658 case P2PS_SETUP_GROUP_OWNER
| P2PS_SETUP_NEW
:
662 case P2PS_SETUP_GROUP_OWNER
:
664 * Must be a complimentary role - cannot be a client to
665 * more than one peer.
667 if (incoming
== role
|| client
)
668 return P2PS_SETUP_NONE
;
670 return P2PS_SETUP_CLIENT
;
672 case P2PS_SETUP_CLIENT
:
673 /* Must be a complimentary role */
674 if (incoming
!= role
) {
675 conncap
= P2PS_SETUP_GROUP_OWNER
;
680 return P2PS_SETUP_NONE
;
685 * For now, we only will support ownership of one group, and being a
686 * client of one group. Therefore, if we have either an existing GO
687 * group, or an existing client group, we will not do a new GO
688 * negotiation, but rather try to re-use the existing groups.
691 case P2PS_SETUP_NONE
:
694 conncap
= P2PS_SETUP_GROUP_OWNER
;
695 else if (!owned_members
)
696 conncap
= P2PS_SETUP_NEW
;
697 else if (incoming
== P2PS_SETUP_NONE
)
698 conncap
= P2PS_SETUP_GROUP_OWNER
| P2PS_SETUP_CLIENT
;
700 conncap
= P2PS_SETUP_CLIENT
;
703 case P2PS_SETUP_CLIENT
:
704 conncap
= P2PS_SETUP_GROUP_OWNER
;
707 case P2PS_SETUP_GROUP_OWNER
:
709 conncap
= P2PS_SETUP_CLIENT
;
712 case P2PS_SETUP_GROUP_OWNER
| P2PS_SETUP_NEW
:
713 case P2PS_SETUP_GROUP_OWNER
| P2PS_SETUP_CLIENT
:
715 conncap
= P2PS_SETUP_GROUP_OWNER
;
719 if (os_get_random(&r
, sizeof(r
)) < 0 ||
721 conncap
= P2PS_SETUP_CLIENT
;
723 conncap
= P2PS_SETUP_GROUP_OWNER
;
728 return P2PS_SETUP_NONE
;
732 if ((conncap
& P2PS_SETUP_GROUP_OWNER
) ||
733 (!incoming
&& (conncap
& P2PS_SETUP_NEW
))) {
734 if (go_wpa_s
&& p2p_client_limit_reached(go_wpa_s
->p2p_group
))
735 conncap
&= ~P2PS_SETUP_GROUP_OWNER
;
736 wpa_printf(MSG_DEBUG
, "P2P: GOs:%d members:%d conncap:%d",
737 owner
, owned_members
, conncap
);
739 s
= wpas_p2p_get_persistent_go(wpa_s
);
741 if (!s
&& !owner
&& p2p_no_group_iface
) {
742 p2p_set_intended_addr(wpa_s
->global
->p2p
,
744 } else if (!s
&& !owner
) {
745 if (wpas_p2p_add_group_interface(wpa_s
,
746 WPA_IF_P2P_GO
) < 0) {
747 wpa_printf(MSG_ERROR
,
748 "P2P: Failed to allocate a new interface for the group");
749 return P2PS_SETUP_NONE
;
751 wpa_s
->global
->pending_group_iface_for_p2ps
= 1;
752 p2p_set_intended_addr(wpa_s
->global
->p2p
,
753 wpa_s
->pending_interface_addr
);
761 static int wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
,
762 enum p2p_group_removal_reason removal_reason
)
764 struct wpa_ssid
*ssid
;
768 ssid
= wpa_s
->current_ssid
;
771 * The current SSID was not known, but there may still be a
772 * pending P2P group interface waiting for provisioning or a
773 * P2P group that is trying to reconnect.
775 ssid
= wpa_s
->conf
->ssid
;
777 if (ssid
->p2p_group
&& ssid
->disabled
!= 2)
782 wpa_s
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)
784 wpa_printf(MSG_ERROR
, "P2P: P2P group interface "
789 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
791 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
792 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
793 wpa_s
->reassociate
= 0;
794 wpa_s
->disconnected
= 1;
799 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
&& ssid
)
800 wpas_notify_p2p_group_removed(wpa_s
, ssid
, gtype
);
802 if (os_strcmp(gtype
, "client") == 0) {
803 wpa_supplicant_deauthenticate(wpa_s
, WLAN_REASON_DEAUTH_LEAVING
);
804 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal
,
806 wpa_printf(MSG_DEBUG
,
807 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
808 removal_reason
= P2P_GROUP_REMOVAL_PSK_FAILURE
;
809 eloop_cancel_timeout(wpas_p2p_psk_failure_removal
,
814 if (wpa_s
->cross_connect_in_use
) {
815 wpa_s
->cross_connect_in_use
= 0;
816 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
817 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
818 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
820 switch (removal_reason
) {
821 case P2P_GROUP_REMOVAL_REQUESTED
:
822 reason
= " reason=REQUESTED";
824 case P2P_GROUP_REMOVAL_FORMATION_FAILED
:
825 reason
= " reason=FORMATION_FAILED";
827 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
828 reason
= " reason=IDLE";
830 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
831 reason
= " reason=UNAVAILABLE";
833 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION
:
834 reason
= " reason=GO_ENDING_SESSION";
836 case P2P_GROUP_REMOVAL_PSK_FAILURE
:
837 reason
= " reason=PSK_FAILURE";
839 case P2P_GROUP_REMOVAL_FREQ_CONFLICT
:
840 reason
= " reason=FREQ_CONFLICT";
846 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
) {
847 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
848 P2P_EVENT_GROUP_REMOVED
"%s %s%s",
849 wpa_s
->ifname
, gtype
, reason
);
852 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict
, wpa_s
, NULL
) > 0)
853 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group freq_conflict timeout");
854 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
855 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
856 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
857 wpa_s
->parent
, NULL
) > 0) {
858 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group formation "
860 wpa_s
->p2p_in_provisioning
= 0;
863 wpa_s
->p2p_in_invitation
= 0;
866 * Make sure wait for the first client does not remain active after the
867 * group has been removed.
869 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
871 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
872 struct wpa_global
*global
;
874 enum wpa_driver_if_type type
;
875 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
877 global
= wpa_s
->global
;
878 ifname
= os_strdup(wpa_s
->ifname
);
879 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
880 eloop_cancel_timeout(run_wpas_p2p_disconnect
, wpa_s
, NULL
);
881 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
, 0);
882 wpa_s
= global
->ifaces
;
884 wpa_drv_if_remove(wpa_s
, type
, ifname
);
889 if (!wpa_s
->p2p_go_group_formation_completed
) {
890 wpa_s
->global
->p2p_group_formation
= NULL
;
891 wpa_s
->p2p_in_provisioning
= 0;
894 wpa_s
->show_group_started
= 0;
895 os_free(wpa_s
->go_params
);
896 wpa_s
->go_params
= NULL
;
898 os_free(wpa_s
->p2p_group_common_freqs
);
899 wpa_s
->p2p_group_common_freqs
= NULL
;
900 wpa_s
->p2p_group_common_freqs_num
= 0;
902 wpa_s
->waiting_presence_resp
= 0;
904 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
905 if (ssid
&& (ssid
->p2p_group
||
906 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
907 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
909 if (ssid
== wpa_s
->current_ssid
) {
910 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
911 eapol_sm_notify_config(wpa_s
->eapol
, NULL
, NULL
);
912 wpa_s
->current_ssid
= NULL
;
915 * Networks objects created during any P2P activities are not
916 * exposed out as they might/will confuse certain non-P2P aware
917 * applications since these network objects won't behave like
920 * Likewise, we don't send out network removed signals for such
923 wpa_config_remove_network(wpa_s
->conf
, id
);
924 wpa_supplicant_clear_status(wpa_s
);
925 wpa_supplicant_cancel_sched_scan(wpa_s
);
927 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
931 wpa_supplicant_ap_deinit(wpa_s
);
933 wpa_drv_deinit_p2p_cli(wpa_s
);
939 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
941 const u8
*ssid
, size_t ssid_len
)
949 if (wpa_s
->go_params
)
950 bssid
= wpa_s
->go_params
->peer_interface_addr
;
952 bssid
= wpa_s
->bssid
;
954 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
955 if (bss
== NULL
&& wpa_s
->go_params
&&
956 !is_zero_ether_addr(wpa_s
->go_params
->peer_device_addr
))
957 bss
= wpa_bss_get_p2p_dev_addr(
958 wpa_s
, wpa_s
->go_params
->peer_device_addr
);
960 u8 iface_addr
[ETH_ALEN
];
961 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
963 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
966 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
967 "group is persistent - BSS " MACSTR
" not found",
972 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
974 p2p
= wpa_bss_get_vendor_ie_multi_beacon(bss
,
977 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
978 "group is persistent - BSS " MACSTR
979 " did not include P2P IE", MAC2STR(bssid
));
980 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
981 (u8
*) (bss
+ 1), bss
->ie_len
);
982 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
983 ((u8
*) bss
+ 1) + bss
->ie_len
,
988 group_capab
= p2p_get_group_capab(p2p
);
989 addr
= p2p_get_go_dev_addr(p2p
);
990 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
991 "group_capab=0x%x", group_capab
);
993 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
994 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
997 os_memset(go_dev_addr
, 0, ETH_ALEN
);
1000 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
1001 "go_dev_addr=" MACSTR
,
1002 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
1004 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
1008 static int wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
1009 struct wpa_ssid
*ssid
,
1010 const u8
*go_dev_addr
)
1015 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
1016 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
1017 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1018 if (s
->disabled
== 2 &&
1019 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
1020 s
->ssid_len
== ssid
->ssid_len
&&
1021 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
1026 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
1028 if (ssid
->passphrase
&& !s
->passphrase
)
1030 else if (ssid
->passphrase
&& s
->passphrase
&&
1031 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
1034 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
1037 s
= wpa_config_add_network(wpa_s
->conf
);
1042 * Instead of network_added we emit persistent_group_added
1043 * notification. Also to keep the defense checks in
1044 * persistent_group obj registration method, we set the
1045 * relevant flags in s to designate it as a persistent group.
1048 s
->p2p_persistent_group
= 1;
1049 wpas_notify_persistent_group_added(wpa_s
, s
);
1050 wpa_config_set_network_defaults(s
);
1054 s
->p2p_persistent_group
= 1;
1057 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
1058 s
->mode
= ssid
->mode
;
1059 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
1060 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
1061 s
->proto
= WPA_PROTO_RSN
;
1062 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
1064 if (ssid
->passphrase
) {
1065 os_free(s
->passphrase
);
1066 s
->passphrase
= os_strdup(ssid
->passphrase
);
1068 if (ssid
->psk_set
) {
1070 os_memcpy(s
->psk
, ssid
->psk
, 32);
1072 if (s
->passphrase
&& !s
->psk_set
)
1073 wpa_config_update_psk(s
);
1074 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
1076 s
->ssid
= os_malloc(ssid
->ssid_len
);
1079 s
->ssid_len
= ssid
->ssid_len
;
1080 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
1082 if (ssid
->mode
== WPAS_MODE_P2P_GO
&& wpa_s
->global
->add_psk
) {
1083 dl_list_add(&s
->psk_list
, &wpa_s
->global
->add_psk
->list
);
1084 wpa_s
->global
->add_psk
= NULL
;
1088 if (changed
&& wpa_s
->conf
->update_config
&&
1089 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
1090 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
1097 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant
*wpa_s
,
1100 struct wpa_ssid
*ssid
, *s
;
1105 ssid
= wpa_s
->current_ssid
;
1106 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
1107 !ssid
->p2p_persistent_group
)
1110 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
1111 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
1114 if (s
->ssid_len
== ssid
->ssid_len
&&
1115 os_memcmp(s
->ssid
, ssid
->ssid
, s
->ssid_len
) == 0)
1122 for (i
= 0; s
->p2p_client_list
&& i
< s
->num_p2p_clients
; i
++) {
1123 if (os_memcmp(s
->p2p_client_list
+ i
* 2 * ETH_ALEN
, addr
,
1127 if (i
== s
->num_p2p_clients
- 1)
1128 return; /* already the most recent entry */
1130 /* move the entry to mark it most recent */
1131 os_memmove(s
->p2p_client_list
+ i
* 2 * ETH_ALEN
,
1132 s
->p2p_client_list
+ (i
+ 1) * 2 * ETH_ALEN
,
1133 (s
->num_p2p_clients
- i
- 1) * 2 * ETH_ALEN
);
1134 os_memcpy(s
->p2p_client_list
+
1135 (s
->num_p2p_clients
- 1) * 2 * ETH_ALEN
, addr
,
1137 os_memset(s
->p2p_client_list
+
1138 (s
->num_p2p_clients
- 1) * 2 * ETH_ALEN
+ ETH_ALEN
,
1144 if (!found
&& s
->num_p2p_clients
< P2P_MAX_STORED_CLIENTS
) {
1145 n
= os_realloc_array(s
->p2p_client_list
,
1146 s
->num_p2p_clients
+ 1, 2 * ETH_ALEN
);
1149 os_memcpy(n
+ s
->num_p2p_clients
* 2 * ETH_ALEN
, addr
,
1151 os_memset(n
+ s
->num_p2p_clients
* 2 * ETH_ALEN
+ ETH_ALEN
,
1153 s
->p2p_client_list
= n
;
1154 s
->num_p2p_clients
++;
1155 } else if (!found
&& s
->p2p_client_list
) {
1156 /* Not enough room for an additional entry - drop the oldest
1158 os_memmove(s
->p2p_client_list
,
1159 s
->p2p_client_list
+ 2 * ETH_ALEN
,
1160 (s
->num_p2p_clients
- 1) * 2 * ETH_ALEN
);
1161 os_memcpy(s
->p2p_client_list
+
1162 (s
->num_p2p_clients
- 1) * 2 * ETH_ALEN
,
1164 os_memset(s
->p2p_client_list
+
1165 (s
->num_p2p_clients
- 1) * 2 * ETH_ALEN
+ ETH_ALEN
,
1169 if (wpa_s
->parent
->conf
->update_config
&&
1170 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
1171 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
1175 static void wpas_p2p_group_started(struct wpa_supplicant
*wpa_s
,
1176 int go
, struct wpa_ssid
*ssid
, int freq
,
1177 const u8
*psk
, const char *passphrase
,
1178 const u8
*go_dev_addr
, int persistent
,
1181 const char *ssid_txt
;
1185 wpa_snprintf_hex(psk_txt
, sizeof(psk_txt
), psk
, 32);
1190 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
1194 if (passphrase
&& passphrase
[0] == '\0')
1198 * Include PSK/passphrase only in the control interface message and
1199 * leave it out from the debug log entry.
1201 wpa_msg_global_ctrl(wpa_s
->parent
, MSG_INFO
,
1202 P2P_EVENT_GROUP_STARTED
1203 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1205 wpa_s
->ifname
, go
? "GO" : "client", ssid_txt
, freq
,
1206 psk
? " psk=" : "", psk_txt
,
1207 passphrase
? " passphrase=\"" : "",
1208 passphrase
? passphrase
: "",
1209 passphrase
? "\"" : "",
1210 MAC2STR(go_dev_addr
),
1211 persistent
? " [PERSISTENT]" : "", extra
);
1212 wpa_printf(MSG_INFO
, P2P_EVENT_GROUP_STARTED
1213 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR
"%s%s",
1214 wpa_s
->ifname
, go
? "GO" : "client", ssid_txt
, freq
,
1215 MAC2STR(go_dev_addr
), persistent
? " [PERSISTENT]" : "",
1220 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
1223 struct wpa_ssid
*ssid
;
1226 u8 go_dev_addr
[ETH_ALEN
];
1227 int network_id
= -1;
1230 * This callback is likely called for the main interface. Update wpa_s
1231 * to use the group interface if a new interface was created for the
1234 if (wpa_s
->global
->p2p_group_formation
)
1235 wpa_s
= wpa_s
->global
->p2p_group_formation
;
1236 if (wpa_s
->p2p_go_group_formation_completed
) {
1237 wpa_s
->global
->p2p_group_formation
= NULL
;
1238 wpa_s
->p2p_in_provisioning
= 0;
1240 wpa_s
->p2p_in_invitation
= 0;
1241 wpa_s
->group_formation_reported
= 1;
1244 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
1245 P2P_EVENT_GROUP_FORMATION_FAILURE
);
1246 wpas_p2p_group_delete(wpa_s
,
1247 P2P_GROUP_REMOVAL_FORMATION_FAILED
);
1251 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
1252 P2P_EVENT_GROUP_FORMATION_SUCCESS
);
1254 ssid
= wpa_s
->current_ssid
;
1255 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
1256 ssid
->mode
= WPAS_MODE_P2P_GO
;
1257 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
1258 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
1263 client
= ssid
->mode
== WPAS_MODE_INFRA
;
1264 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
1265 persistent
= ssid
->p2p_persistent_group
;
1266 os_memcpy(go_dev_addr
, wpa_s
->global
->p2p_dev_addr
,
1269 persistent
= wpas_p2p_persistent_group(wpa_s
,
1274 client
= wpa_s
->p2p_group_interface
==
1275 P2P_GROUP_INTERFACE_CLIENT
;
1276 os_memset(go_dev_addr
, 0, ETH_ALEN
);
1279 wpa_s
->show_group_started
= 0;
1282 * Indicate event only after successfully completed 4-way
1283 * handshake, i.e., when the interface is ready for data
1286 wpa_s
->show_group_started
= 1;
1288 wpas_p2p_group_started(wpa_s
, 1, ssid
,
1289 ssid
? ssid
->frequency
: 0,
1290 ssid
&& ssid
->passphrase
== NULL
&&
1291 ssid
->psk_set
? ssid
->psk
: NULL
,
1292 ssid
? ssid
->passphrase
: NULL
,
1293 go_dev_addr
, persistent
, "");
1294 wpas_p2p_cross_connect_setup(wpa_s
);
1295 wpas_p2p_set_group_idle_timeout(wpa_s
);
1299 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
1302 os_free(wpa_s
->global
->add_psk
);
1303 wpa_s
->global
->add_psk
= NULL
;
1305 if (network_id
< 0 && ssid
)
1306 network_id
= ssid
->id
;
1308 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
1309 os_get_reltime(&wpa_s
->global
->p2p_go_wait_client
);
1314 struct send_action_work
{
1320 unsigned int wait_time
;
1325 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx
,
1328 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1330 if (!wpa_s
->p2p_send_action_work
)
1333 wpa_printf(MSG_DEBUG
, "P2P: Send Action frame radio work timed out");
1334 os_free(wpa_s
->p2p_send_action_work
->ctx
);
1335 radio_work_done(wpa_s
->p2p_send_action_work
);
1336 wpa_s
->p2p_send_action_work
= NULL
;
1340 static void wpas_p2p_action_tx_clear(struct wpa_supplicant
*wpa_s
)
1342 if (wpa_s
->p2p_send_action_work
) {
1343 struct send_action_work
*awork
;
1344 awork
= wpa_s
->p2p_send_action_work
->ctx
;
1345 if (awork
->wait_time
== 0) {
1347 radio_work_done(wpa_s
->p2p_send_action_work
);
1348 wpa_s
->p2p_send_action_work
= NULL
;
1351 * In theory, this should not be needed, but number of
1352 * places in the P2P code is still using non-zero wait
1353 * time for the last Action frame in the sequence and
1354 * some of these do not call send_action_done().
1356 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout
,
1358 eloop_register_timeout(
1359 0, awork
->wait_time
* 1000,
1360 wpas_p2p_send_action_work_timeout
,
1367 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant
*wpa_s
,
1369 const u8
*dst
, const u8
*src
,
1371 const u8
*data
, size_t data_len
,
1372 enum offchannel_send_action_result
1375 enum p2p_send_action_result res
= P2P_SEND_ACTION_SUCCESS
;
1377 wpas_p2p_action_tx_clear(wpa_s
);
1379 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
1383 case OFFCHANNEL_SEND_ACTION_SUCCESS
:
1384 res
= P2P_SEND_ACTION_SUCCESS
;
1386 case OFFCHANNEL_SEND_ACTION_NO_ACK
:
1387 res
= P2P_SEND_ACTION_NO_ACK
;
1389 case OFFCHANNEL_SEND_ACTION_FAILED
:
1390 res
= P2P_SEND_ACTION_FAILED
;
1394 p2p_send_action_cb(wpa_s
->global
->p2p
, freq
, dst
, src
, bssid
, res
);
1396 if (result
!= OFFCHANNEL_SEND_ACTION_SUCCESS
&&
1397 wpa_s
->pending_pd_before_join
&&
1398 (os_memcmp(dst
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
1399 os_memcmp(dst
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0) &&
1400 wpa_s
->p2p_fallback_to_go_neg
) {
1401 wpa_s
->pending_pd_before_join
= 0;
1402 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No ACK for PD Req "
1403 "during p2p_connect-auto");
1404 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
1405 P2P_EVENT_FALLBACK_TO_GO_NEG
1406 "reason=no-ACK-to-PD-Req");
1407 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
1413 static void wpas_send_action_cb(struct wpa_radio_work
*work
, int deinit
)
1415 struct wpa_supplicant
*wpa_s
= work
->wpa_s
;
1416 struct send_action_work
*awork
= work
->ctx
;
1419 if (work
->started
) {
1420 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout
,
1422 wpa_s
->p2p_send_action_work
= NULL
;
1423 offchannel_send_action_done(wpa_s
);
1429 if (offchannel_send_action(wpa_s
, awork
->freq
, awork
->dst
, awork
->src
,
1430 awork
->bssid
, awork
->buf
, awork
->len
,
1432 wpas_p2p_send_action_tx_status
, 1) < 0) {
1434 radio_work_done(work
);
1437 wpa_s
->p2p_send_action_work
= work
;
1441 static int wpas_send_action_work(struct wpa_supplicant
*wpa_s
,
1442 unsigned int freq
, const u8
*dst
,
1443 const u8
*src
, const u8
*bssid
, const u8
*buf
,
1444 size_t len
, unsigned int wait_time
)
1446 struct send_action_work
*awork
;
1448 if (wpa_s
->p2p_send_action_work
) {
1449 wpa_printf(MSG_DEBUG
, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1453 awork
= os_zalloc(sizeof(*awork
) + len
);
1458 os_memcpy(awork
->dst
, dst
, ETH_ALEN
);
1459 os_memcpy(awork
->src
, src
, ETH_ALEN
);
1460 os_memcpy(awork
->bssid
, bssid
, ETH_ALEN
);
1462 awork
->wait_time
= wait_time
;
1463 os_memcpy(awork
->buf
, buf
, len
);
1465 if (radio_add_work(wpa_s
, freq
, "p2p-send-action", 0,
1466 wpas_send_action_cb
, awork
) < 0) {
1475 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
1476 const u8
*src
, const u8
*bssid
, const u8
*buf
,
1477 size_t len
, unsigned int wait_time
)
1479 struct wpa_supplicant
*wpa_s
= ctx
;
1480 int listen_freq
= -1, send_freq
= -1;
1482 if (wpa_s
->p2p_listen_work
)
1483 listen_freq
= wpa_s
->p2p_listen_work
->freq
;
1484 if (wpa_s
->p2p_send_action_work
)
1485 send_freq
= wpa_s
->p2p_send_action_work
->freq
;
1486 if (listen_freq
!= (int) freq
&& send_freq
!= (int) freq
) {
1487 wpa_printf(MSG_DEBUG
, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1488 listen_freq
, send_freq
);
1489 return wpas_send_action_work(wpa_s
, freq
, dst
, src
, bssid
, buf
,
1493 wpa_printf(MSG_DEBUG
, "P2P: Use ongoing radio work for Action frame TX");
1494 return offchannel_send_action(wpa_s
, freq
, dst
, src
, bssid
, buf
, len
,
1496 wpas_p2p_send_action_tx_status
, 1);
1500 static void wpas_send_action_done(void *ctx
)
1502 struct wpa_supplicant
*wpa_s
= ctx
;
1504 if (wpa_s
->p2p_send_action_work
) {
1505 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout
,
1507 os_free(wpa_s
->p2p_send_action_work
->ctx
);
1508 radio_work_done(wpa_s
->p2p_send_action_work
);
1509 wpa_s
->p2p_send_action_work
= NULL
;
1512 offchannel_send_action_done(wpa_s
);
1516 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
1517 struct p2p_go_neg_results
*params
)
1519 if (wpa_s
->go_params
== NULL
) {
1520 wpa_s
->go_params
= os_malloc(sizeof(*params
));
1521 if (wpa_s
->go_params
== NULL
)
1524 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
1529 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
1530 struct p2p_go_neg_results
*res
)
1532 wpa_s
->group_formation_reported
= 0;
1533 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
1534 " dev_addr " MACSTR
" wps_method %d",
1535 MAC2STR(res
->peer_interface_addr
),
1536 MAC2STR(res
->peer_device_addr
), res
->wps_method
);
1537 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
1538 res
->ssid
, res
->ssid_len
);
1539 wpa_supplicant_ap_deinit(wpa_s
);
1540 wpas_copy_go_neg_results(wpa_s
, res
);
1541 if (res
->wps_method
== WPS_PBC
) {
1542 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
1543 #ifdef CONFIG_WPS_NFC
1544 } else if (res
->wps_method
== WPS_NFC
) {
1545 wpas_wps_start_nfc(wpa_s
, res
->peer_device_addr
,
1546 res
->peer_interface_addr
,
1547 wpa_s
->parent
->p2p_oob_dev_pw
,
1548 wpa_s
->parent
->p2p_oob_dev_pw_id
, 1,
1549 wpa_s
->parent
->p2p_oob_dev_pw_id
==
1550 DEV_PW_NFC_CONNECTION_HANDOVER
?
1551 wpa_s
->parent
->p2p_peer_oob_pubkey_hash
:
1554 #endif /* CONFIG_WPS_NFC */
1556 u16 dev_pw_id
= DEV_PW_DEFAULT
;
1557 if (wpa_s
->p2p_wps_method
== WPS_P2PS
)
1558 dev_pw_id
= DEV_PW_P2PS_DEFAULT
;
1559 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
1560 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
1561 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
1562 wpa_s
->p2p_pin
, 1, dev_pw_id
);
1567 static void wpas_p2p_add_psk_list(struct wpa_supplicant
*wpa_s
,
1568 struct wpa_ssid
*ssid
)
1570 struct wpa_ssid
*persistent
;
1571 struct psk_list_entry
*psk
;
1572 struct hostapd_data
*hapd
;
1574 if (!wpa_s
->ap_iface
)
1577 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, NULL
, ssid
->ssid
,
1579 if (persistent
== NULL
)
1582 hapd
= wpa_s
->ap_iface
->bss
[0];
1584 dl_list_for_each(psk
, &persistent
->psk_list
, struct psk_list_entry
,
1586 struct hostapd_wpa_psk
*hpsk
;
1588 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add persistent group PSK entry for "
1590 MAC2STR(psk
->addr
), psk
->p2p
);
1591 hpsk
= os_zalloc(sizeof(*hpsk
));
1594 os_memcpy(hpsk
->psk
, psk
->psk
, PMK_LEN
);
1596 os_memcpy(hpsk
->p2p_dev_addr
, psk
->addr
, ETH_ALEN
);
1598 os_memcpy(hpsk
->addr
, psk
->addr
, ETH_ALEN
);
1599 hpsk
->next
= hapd
->conf
->ssid
.wpa_psk
;
1600 hapd
->conf
->ssid
.wpa_psk
= hpsk
;
1605 static void p2p_go_dump_common_freqs(struct wpa_supplicant
*wpa_s
)
1609 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Common group frequencies (len=%u):",
1610 wpa_s
->p2p_group_common_freqs_num
);
1612 for (i
= 0; i
< wpa_s
->p2p_group_common_freqs_num
; i
++)
1613 wpa_dbg(wpa_s
, MSG_DEBUG
, "freq[%u]: %d",
1614 i
, wpa_s
->p2p_group_common_freqs
[i
]);
1618 static void p2p_go_save_group_common_freqs(struct wpa_supplicant
*wpa_s
,
1619 struct p2p_go_neg_results
*params
)
1621 unsigned int i
, len
= int_array_len(wpa_s
->go_params
->freq_list
);
1623 wpa_s
->p2p_group_common_freqs_num
= 0;
1624 os_free(wpa_s
->p2p_group_common_freqs
);
1625 wpa_s
->p2p_group_common_freqs
= os_calloc(len
, sizeof(int));
1626 if (!wpa_s
->p2p_group_common_freqs
)
1629 for (i
= 0; i
< len
; i
++) {
1630 if (!wpa_s
->go_params
->freq_list
[i
])
1632 wpa_s
->p2p_group_common_freqs
[i
] =
1633 wpa_s
->go_params
->freq_list
[i
];
1635 wpa_s
->p2p_group_common_freqs_num
= i
;
1639 static void p2p_config_write(struct wpa_supplicant
*wpa_s
)
1641 #ifndef CONFIG_NO_CONFIG_WRITE
1642 if (wpa_s
->parent
->conf
->update_config
&&
1643 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
1644 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
1645 #endif /* CONFIG_NO_CONFIG_WRITE */
1649 static void p2p_go_configured(void *ctx
, void *data
)
1651 struct wpa_supplicant
*wpa_s
= ctx
;
1652 struct p2p_go_neg_results
*params
= data
;
1653 struct wpa_ssid
*ssid
;
1654 int network_id
= -1;
1656 p2p_go_save_group_common_freqs(wpa_s
, params
);
1657 p2p_go_dump_common_freqs(wpa_s
);
1659 ssid
= wpa_s
->current_ssid
;
1660 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
1661 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
1662 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
1663 wpa_s
->global
->p2p_group_formation
= NULL
;
1664 wpas_p2p_group_started(wpa_s
, 1, ssid
, ssid
->frequency
,
1665 params
->passphrase
[0] == '\0' ?
1668 wpa_s
->global
->p2p_dev_addr
,
1669 params
->persistent_group
, "");
1670 wpa_s
->group_formation_reported
= 1;
1672 if (wpa_s
->parent
->p2ps_join_addr_valid
) {
1673 wpa_dbg(wpa_s
, MSG_DEBUG
,
1674 "P2PS: Setting default PIN for " MACSTR
,
1675 MAC2STR(wpa_s
->parent
->p2ps_join_addr
));
1676 wpa_supplicant_ap_wps_pin(wpa_s
,
1677 wpa_s
->parent
->p2ps_join_addr
,
1678 "12345670", NULL
, 0, 0);
1679 wpa_s
->parent
->p2ps_join_addr_valid
= 0;
1682 os_get_reltime(&wpa_s
->global
->p2p_go_wait_client
);
1683 if (params
->persistent_group
) {
1684 network_id
= wpas_p2p_store_persistent_group(
1685 wpa_s
->parent
, ssid
,
1686 wpa_s
->global
->p2p_dev_addr
);
1687 wpas_p2p_add_psk_list(wpa_s
, ssid
);
1690 network_id
= ssid
->id
;
1691 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
1692 wpas_p2p_cross_connect_setup(wpa_s
);
1693 wpas_p2p_set_group_idle_timeout(wpa_s
);
1695 if (wpa_s
->p2p_first_connection_timeout
) {
1696 wpa_dbg(wpa_s
, MSG_DEBUG
,
1697 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1698 wpa_s
->p2p_first_connection_timeout
);
1699 wpa_s
->p2p_go_group_formation_completed
= 0;
1700 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1701 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
1702 wpa_s
->parent
, NULL
);
1703 eloop_register_timeout(
1704 wpa_s
->p2p_first_connection_timeout
, 0,
1705 wpas_p2p_group_formation_timeout
,
1706 wpa_s
->parent
, NULL
);
1712 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
1713 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
1714 params
->peer_interface_addr
)) {
1715 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
1719 if (params
->wps_method
== WPS_PBC
) {
1720 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
1721 params
->peer_device_addr
);
1722 #ifdef CONFIG_WPS_NFC
1723 } else if (params
->wps_method
== WPS_NFC
) {
1724 if (wpa_s
->parent
->p2p_oob_dev_pw_id
!=
1725 DEV_PW_NFC_CONNECTION_HANDOVER
&&
1726 !wpa_s
->parent
->p2p_oob_dev_pw
) {
1727 wpa_printf(MSG_DEBUG
, "P2P: No NFC Dev Pw known");
1730 wpas_ap_wps_add_nfc_pw(
1731 wpa_s
, wpa_s
->parent
->p2p_oob_dev_pw_id
,
1732 wpa_s
->parent
->p2p_oob_dev_pw
,
1733 wpa_s
->parent
->p2p_peer_oob_pk_hash_known
?
1734 wpa_s
->parent
->p2p_peer_oob_pubkey_hash
: NULL
);
1735 #endif /* CONFIG_WPS_NFC */
1736 } else if (wpa_s
->p2p_pin
[0])
1737 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
1738 wpa_s
->p2p_pin
, NULL
, 0, 0);
1739 os_free(wpa_s
->go_params
);
1740 wpa_s
->go_params
= NULL
;
1744 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
1745 struct p2p_go_neg_results
*params
,
1746 int group_formation
)
1748 struct wpa_ssid
*ssid
;
1750 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Starting GO");
1751 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0) {
1752 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not copy GO Negotiation "
1757 ssid
= wpa_config_add_network(wpa_s
->conf
);
1759 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not add network for GO");
1763 wpa_s
->show_group_started
= 0;
1764 wpa_s
->p2p_go_group_formation_completed
= 0;
1765 wpa_s
->group_formation_reported
= 0;
1767 wpa_config_set_network_defaults(ssid
);
1768 ssid
->temporary
= 1;
1769 ssid
->p2p_group
= 1;
1770 ssid
->p2p_persistent_group
= params
->persistent_group
;
1771 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
1773 ssid
->frequency
= params
->freq
;
1774 ssid
->ht40
= params
->ht40
;
1775 ssid
->vht
= params
->vht
;
1776 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
1778 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
1779 ssid
->ssid_len
= params
->ssid_len
;
1781 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
1782 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
1783 ssid
->proto
= WPA_PROTO_RSN
;
1784 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
1785 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
1786 if (params
->freq
> 56160) {
1788 * Enable GCMP instead of CCMP as pairwise_cipher and
1789 * group_cipher in 60 GHz.
1791 ssid
->pairwise_cipher
= WPA_CIPHER_GCMP
;
1792 ssid
->group_cipher
= WPA_CIPHER_GCMP
;
1794 if (os_strlen(params
->passphrase
) > 0) {
1795 ssid
->passphrase
= os_strdup(params
->passphrase
);
1796 if (ssid
->passphrase
== NULL
) {
1797 wpa_msg_global(wpa_s
, MSG_ERROR
,
1798 "P2P: Failed to copy passphrase for GO");
1799 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1803 ssid
->passphrase
= NULL
;
1804 ssid
->psk_set
= params
->psk_set
;
1806 os_memcpy(ssid
->psk
, params
->psk
, sizeof(ssid
->psk
));
1807 else if (ssid
->passphrase
)
1808 wpa_config_update_psk(ssid
);
1809 ssid
->ap_max_inactivity
= wpa_s
->parent
->conf
->p2p_go_max_inactivity
;
1811 wpa_s
->ap_configured_cb
= p2p_go_configured
;
1812 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
1813 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
1814 wpa_s
->scan_req
= NORMAL_SCAN_REQ
;
1815 wpa_s
->connect_without_scan
= ssid
;
1816 wpa_s
->reassociate
= 1;
1817 wpa_s
->disconnected
= 0;
1818 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Request scan (that will be skipped) to "
1820 wpa_supplicant_req_scan(wpa_s
, 0, 0);
1824 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
1825 const struct wpa_supplicant
*src
)
1827 struct wpa_config
*d
;
1828 const struct wpa_config
*s
;
1833 #define C(n) if (s->n) d->n = os_strdup(s->n)
1842 os_memcpy(d
->device_type
, s
->device_type
, WPS_DEV_TYPE_LEN
);
1843 os_memcpy(d
->sec_device_type
, s
->sec_device_type
,
1844 sizeof(d
->sec_device_type
));
1845 d
->num_sec_device_types
= s
->num_sec_device_types
;
1847 d
->p2p_group_idle
= s
->p2p_group_idle
;
1848 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
1849 d
->persistent_reconnect
= s
->persistent_reconnect
;
1850 d
->max_num_sta
= s
->max_num_sta
;
1851 d
->pbc_in_m1
= s
->pbc_in_m1
;
1852 d
->ignore_old_scan_res
= s
->ignore_old_scan_res
;
1853 d
->beacon_int
= s
->beacon_int
;
1854 d
->dtim_period
= s
->dtim_period
;
1855 d
->disassoc_low_ack
= s
->disassoc_low_ack
;
1856 d
->disable_scan_offload
= s
->disable_scan_offload
;
1857 d
->passive_scan
= s
->passive_scan
;
1859 if (s
->wps_nfc_dh_privkey
&& s
->wps_nfc_dh_pubkey
) {
1860 d
->wps_nfc_dh_privkey
= wpabuf_dup(s
->wps_nfc_dh_privkey
);
1861 d
->wps_nfc_dh_pubkey
= wpabuf_dup(s
->wps_nfc_dh_pubkey
);
1866 static void wpas_p2p_get_group_ifname(struct wpa_supplicant
*wpa_s
,
1867 char *ifname
, size_t len
)
1869 char *ifname_ptr
= wpa_s
->ifname
;
1871 if (os_strncmp(wpa_s
->ifname
, P2P_MGMT_DEVICE_PREFIX
,
1872 os_strlen(P2P_MGMT_DEVICE_PREFIX
)) == 0) {
1873 ifname_ptr
= os_strrchr(wpa_s
->ifname
, '-') + 1;
1876 os_snprintf(ifname
, len
, "p2p-%s-%d", ifname_ptr
, wpa_s
->p2p_group_idx
);
1877 if (os_strlen(ifname
) >= IFNAMSIZ
&&
1878 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
1881 /* Try to avoid going over the IFNAMSIZ length limit */
1882 res
= os_snprintf(ifname
, len
, "p2p-%d", wpa_s
->p2p_group_idx
);
1883 if (os_snprintf_error(len
, res
) && len
)
1884 ifname
[len
- 1] = '\0';
1889 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
1890 enum wpa_driver_if_type type
)
1892 char ifname
[120], force_ifname
[120];
1894 if (wpa_s
->pending_interface_name
[0]) {
1895 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
1896 "- skip creation of a new one");
1897 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
1898 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
1899 "unknown?! ifname='%s'",
1900 wpa_s
->pending_interface_name
);
1906 wpas_p2p_get_group_ifname(wpa_s
, ifname
, sizeof(ifname
));
1907 force_ifname
[0] = '\0';
1909 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
1911 wpa_s
->p2p_group_idx
++;
1913 wpa_s
->pending_interface_type
= type
;
1914 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
1915 wpa_s
->pending_interface_addr
, NULL
) < 0) {
1916 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
1921 if (force_ifname
[0]) {
1922 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
1924 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
1925 sizeof(wpa_s
->pending_interface_name
));
1927 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
1928 sizeof(wpa_s
->pending_interface_name
));
1929 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
1930 MACSTR
, wpa_s
->pending_interface_name
,
1931 MAC2STR(wpa_s
->pending_interface_addr
));
1937 static void wpas_p2p_remove_pending_group_interface(
1938 struct wpa_supplicant
*wpa_s
)
1940 if (!wpa_s
->pending_interface_name
[0] ||
1941 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
1942 return; /* No pending virtual interface */
1944 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
1945 wpa_s
->pending_interface_name
);
1946 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
1947 wpa_s
->pending_interface_name
);
1948 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1949 wpa_s
->pending_interface_name
[0] = '\0';
1950 wpa_s
->global
->pending_group_iface_for_p2ps
= 0;
1954 static struct wpa_supplicant
*
1955 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1957 struct wpa_interface iface
;
1958 struct wpa_supplicant
*group_wpa_s
;
1960 if (!wpa_s
->pending_interface_name
[0]) {
1961 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1962 if (!wpas_p2p_create_iface(wpa_s
))
1965 * Something has forced us to remove the pending interface; try
1966 * to create a new one and hope for the best that we will get
1967 * the same local address.
1969 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1970 WPA_IF_P2P_CLIENT
) < 0)
1974 os_memset(&iface
, 0, sizeof(iface
));
1975 iface
.ifname
= wpa_s
->pending_interface_name
;
1976 iface
.driver
= wpa_s
->driver
->name
;
1977 if (wpa_s
->conf
->ctrl_interface
== NULL
&&
1978 wpa_s
->parent
!= wpa_s
&&
1980 (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE
))
1981 iface
.ctrl_interface
= wpa_s
->parent
->conf
->ctrl_interface
;
1983 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1984 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1985 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1986 if (group_wpa_s
== NULL
) {
1987 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1988 "wpa_supplicant interface");
1991 wpa_s
->pending_interface_name
[0] = '\0';
1992 group_wpa_s
->parent
= wpa_s
;
1993 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1994 P2P_GROUP_INTERFACE_CLIENT
;
1995 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1996 wpa_s
->global
->pending_group_iface_for_p2ps
= 0;
1998 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
2004 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
2007 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
2008 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
2009 wpas_p2p_group_formation_failed(wpa_s
);
2013 void wpas_p2p_group_formation_failed(struct wpa_supplicant
*wpa_s
)
2015 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
2016 wpa_s
->parent
, NULL
);
2017 if (wpa_s
->global
->p2p
)
2018 p2p_group_formation_failed(wpa_s
->global
->p2p
);
2019 wpas_group_formation_completed(wpa_s
, 0);
2023 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant
*wpa_s
)
2025 wpa_printf(MSG_DEBUG
, "P2P: Reject group formation due to WPS provisioning failure");
2026 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
2027 wpa_s
->parent
, NULL
);
2028 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
2029 wpa_s
->parent
, NULL
);
2030 wpa_s
->global
->p2p_fail_on_wps_complete
= 0;
2034 void wpas_p2p_ap_setup_failed(struct wpa_supplicant
*wpa_s
)
2036 if (wpa_s
->global
->p2p_group_formation
!= wpa_s
)
2038 /* Speed up group formation timeout since this cannot succeed */
2039 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
2040 wpa_s
->parent
, NULL
);
2041 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
2042 wpa_s
->parent
, NULL
);
2046 static void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
2048 struct wpa_supplicant
*wpa_s
= ctx
;
2050 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
2051 wpa_drv_cancel_remain_on_channel(wpa_s
);
2052 wpa_s
->off_channel_freq
= 0;
2053 wpa_s
->roc_waiting_drv_freq
= 0;
2057 wpa_msg_global(wpa_s
, MSG_INFO
,
2058 P2P_EVENT_GO_NEG_FAILURE
"status=%d",
2060 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
2061 wpas_p2p_remove_pending_group_interface(wpa_s
);
2065 if (wpa_s
->p2p_go_ht40
)
2067 if (wpa_s
->p2p_go_vht
)
2070 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
"role=%s "
2071 "freq=%d ht40=%d peer_dev=" MACSTR
" peer_iface=" MACSTR
2073 res
->role_go
? "GO" : "client", res
->freq
, res
->ht40
,
2074 MAC2STR(res
->peer_device_addr
),
2075 MAC2STR(res
->peer_interface_addr
),
2076 p2p_wps_method_text(res
->wps_method
));
2077 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
2079 if (res
->role_go
&& wpa_s
->p2p_persistent_id
>= 0) {
2080 struct wpa_ssid
*ssid
;
2081 ssid
= wpa_config_get_network(wpa_s
->conf
,
2082 wpa_s
->p2p_persistent_id
);
2083 if (ssid
&& ssid
->disabled
== 2 &&
2084 ssid
->mode
== WPAS_MODE_P2P_GO
&& ssid
->passphrase
) {
2085 size_t len
= os_strlen(ssid
->passphrase
);
2086 wpa_printf(MSG_DEBUG
, "P2P: Override passphrase based "
2087 "on requested persistent group");
2088 os_memcpy(res
->passphrase
, ssid
->passphrase
, len
);
2089 res
->passphrase
[len
] = '\0';
2093 if (wpa_s
->create_p2p_iface
) {
2094 struct wpa_supplicant
*group_wpa_s
=
2095 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
2096 if (group_wpa_s
== NULL
) {
2097 wpas_p2p_remove_pending_group_interface(wpa_s
);
2098 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
,
2100 wpas_p2p_group_formation_failed(wpa_s
);
2103 if (group_wpa_s
!= wpa_s
) {
2104 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
2105 sizeof(group_wpa_s
->p2p_pin
));
2106 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
2108 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
2109 wpa_s
->pending_interface_name
[0] = '\0';
2110 group_wpa_s
->p2p_in_provisioning
= 1;
2113 wpas_start_wps_go(group_wpa_s
, res
, 1);
2115 wpas_start_wps_enrollee(group_wpa_s
, res
);
2117 wpa_s
->p2p_in_provisioning
= 1;
2118 wpa_s
->global
->p2p_group_formation
= wpa_s
;
2121 wpas_start_wps_go(wpa_s
, res
, 1);
2123 wpas_start_wps_enrollee(ctx
, res
);
2126 wpa_s
->p2p_long_listen
= 0;
2127 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
2129 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2130 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
2131 (res
->peer_config_timeout
% 100) * 10000,
2132 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2136 static void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
2138 struct wpa_supplicant
*wpa_s
= ctx
;
2139 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
2140 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
2142 wpas_notify_p2p_go_neg_req(wpa_s
, src
, dev_passwd_id
);
2146 static void wpas_dev_found(void *ctx
, const u8
*addr
,
2147 const struct p2p_peer_info
*info
,
2150 #ifndef CONFIG_NO_STDOUT_DEBUG
2151 struct wpa_supplicant
*wpa_s
= ctx
;
2152 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
2153 char *wfd_dev_info_hex
= NULL
;
2155 #ifdef CONFIG_WIFI_DISPLAY
2156 wfd_dev_info_hex
= wifi_display_subelem_hex(info
->wfd_subelems
,
2157 WFD_SUBELEM_DEVICE_INFO
);
2158 #endif /* CONFIG_WIFI_DISPLAY */
2160 if (info
->p2ps_instance
) {
2162 const u8
*buf
= wpabuf_head(info
->p2ps_instance
);
2163 size_t len
= wpabuf_len(info
->p2ps_instance
);
2170 if (len
< 4 + 2 + 1)
2172 id
= WPA_GET_LE32(buf
);
2174 methods
= WPA_GET_BE16(buf
);
2177 if (str_len
> len
- 4 - 2 - 1)
2179 os_memcpy(str
, buf
, str_len
);
2180 str
[str_len
] = '\0';
2182 len
-= str_len
+ sizeof(u32
) + sizeof(u16
) + sizeof(u8
);
2184 wpa_msg_global(wpa_s
, MSG_INFO
,
2185 P2P_EVENT_DEVICE_FOUND MACSTR
2186 " p2p_dev_addr=" MACSTR
2187 " pri_dev_type=%s name='%s'"
2188 " config_methods=0x%x"
2191 " adv_id=%x asp_svc=%s%s",
2193 MAC2STR(info
->p2p_device_addr
),
2194 wps_dev_type_bin2str(
2196 devtype
, sizeof(devtype
)),
2197 info
->device_name
, methods
,
2198 info
->dev_capab
, info
->group_capab
,
2200 info
->vendor_elems
?
2201 " vendor_elems=1" : "");
2206 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
2207 " p2p_dev_addr=" MACSTR
2208 " pri_dev_type=%s name='%s' config_methods=0x%x "
2209 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2210 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
2211 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
2213 info
->device_name
, info
->config_methods
,
2214 info
->dev_capab
, info
->group_capab
,
2215 wfd_dev_info_hex
? " wfd_dev_info=0x" : "",
2216 wfd_dev_info_hex
? wfd_dev_info_hex
: "",
2217 info
->vendor_elems
? " vendor_elems=1" : "",
2221 os_free(wfd_dev_info_hex
);
2222 #endif /* CONFIG_NO_STDOUT_DEBUG */
2224 wpas_notify_p2p_device_found(ctx
, info
->p2p_device_addr
, new_device
);
2228 static void wpas_dev_lost(void *ctx
, const u8
*dev_addr
)
2230 struct wpa_supplicant
*wpa_s
= ctx
;
2232 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_LOST
2233 "p2p_dev_addr=" MACSTR
, MAC2STR(dev_addr
));
2235 wpas_notify_p2p_device_lost(wpa_s
, dev_addr
);
2239 static void wpas_find_stopped(void *ctx
)
2241 struct wpa_supplicant
*wpa_s
= ctx
;
2242 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_FIND_STOPPED
);
2246 struct wpas_p2p_listen_work
{
2248 unsigned int duration
;
2249 struct wpabuf
*probe_resp_ie
;
2253 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work
*lwork
)
2257 wpabuf_free(lwork
->probe_resp_ie
);
2262 static void wpas_p2p_listen_work_done(struct wpa_supplicant
*wpa_s
)
2264 struct wpas_p2p_listen_work
*lwork
;
2266 if (!wpa_s
->p2p_listen_work
)
2269 lwork
= wpa_s
->p2p_listen_work
->ctx
;
2270 wpas_p2p_listen_work_free(lwork
);
2271 radio_work_done(wpa_s
->p2p_listen_work
);
2272 wpa_s
->p2p_listen_work
= NULL
;
2276 static void wpas_start_listen_cb(struct wpa_radio_work
*work
, int deinit
)
2278 struct wpa_supplicant
*wpa_s
= work
->wpa_s
;
2279 struct wpas_p2p_listen_work
*lwork
= work
->ctx
;
2280 unsigned int duration
;
2283 if (work
->started
) {
2284 wpa_s
->p2p_listen_work
= NULL
;
2285 wpas_stop_listen(wpa_s
);
2287 wpas_p2p_listen_work_free(lwork
);
2291 wpa_s
->p2p_listen_work
= work
;
2293 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, lwork
->probe_resp_ie
, NULL
);
2295 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
2296 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
2297 "report received Probe Request frames");
2298 wpas_p2p_listen_work_done(wpa_s
);
2302 wpa_s
->pending_listen_freq
= lwork
->freq
;
2303 wpa_s
->pending_listen_duration
= lwork
->duration
;
2305 duration
= lwork
->duration
;
2306 #ifdef CONFIG_TESTING_OPTIONS
2307 if (wpa_s
->extra_roc_dur
) {
2308 wpa_printf(MSG_DEBUG
, "TESTING: Increase ROC duration %u -> %u",
2309 duration
, duration
+ wpa_s
->extra_roc_dur
);
2310 duration
+= wpa_s
->extra_roc_dur
;
2312 #endif /* CONFIG_TESTING_OPTIONS */
2314 if (wpa_drv_remain_on_channel(wpa_s
, lwork
->freq
, duration
) < 0) {
2315 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
2316 "to remain on channel (%u MHz) for Listen "
2317 "state", lwork
->freq
);
2318 wpas_p2p_listen_work_done(wpa_s
);
2319 wpa_s
->pending_listen_freq
= 0;
2322 wpa_s
->off_channel_freq
= 0;
2323 wpa_s
->roc_waiting_drv_freq
= lwork
->freq
;
2327 static int wpas_start_listen(void *ctx
, unsigned int freq
,
2328 unsigned int duration
,
2329 const struct wpabuf
*probe_resp_ie
)
2331 struct wpa_supplicant
*wpa_s
= ctx
;
2332 struct wpas_p2p_listen_work
*lwork
;
2334 if (wpa_s
->p2p_listen_work
) {
2335 wpa_printf(MSG_DEBUG
, "P2P: Reject start_listen since p2p_listen_work already exists");
2339 lwork
= os_zalloc(sizeof(*lwork
));
2343 lwork
->duration
= duration
;
2344 if (probe_resp_ie
) {
2345 lwork
->probe_resp_ie
= wpabuf_dup(probe_resp_ie
);
2346 if (lwork
->probe_resp_ie
== NULL
) {
2347 wpas_p2p_listen_work_free(lwork
);
2352 if (radio_add_work(wpa_s
, freq
, "p2p-listen", 0, wpas_start_listen_cb
,
2354 wpas_p2p_listen_work_free(lwork
);
2362 static void wpas_stop_listen(void *ctx
)
2364 struct wpa_supplicant
*wpa_s
= ctx
;
2365 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
2366 wpa_drv_cancel_remain_on_channel(wpa_s
);
2367 wpa_s
->off_channel_freq
= 0;
2368 wpa_s
->roc_waiting_drv_freq
= 0;
2370 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
2371 wpa_drv_probe_req_report(wpa_s
, 0);
2372 wpas_p2p_listen_work_done(wpa_s
);
2376 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
2378 struct wpa_supplicant
*wpa_s
= ctx
;
2379 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
), 1);
2384 * DNS Header section is used only to calculate compression pointers, so the
2385 * contents of this data does not matter, but the length needs to be reserved
2386 * in the virtual packet.
2388 #define DNS_HEADER_LEN 12
2391 * 27-octet in-memory packet from P2P specification containing two implied
2392 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
2394 #define P2P_SD_IN_MEMORY_LEN 27
2396 static int p2p_sd_dns_uncompress_label(char **upos
, char *uend
, u8
*start
,
2397 u8
**spos
, const u8
*end
)
2399 while (*spos
< end
) {
2400 u8 val
= ((*spos
)[0] & 0xc0) >> 6;
2403 if (val
== 1 || val
== 2) {
2404 /* These are reserved values in RFC 1035 */
2405 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
2406 "sequence starting with 0x%x", val
);
2415 if (*spos
+ 2 > end
) {
2416 wpa_printf(MSG_DEBUG
, "P2P: No room for full "
2417 "DNS offset field");
2421 offset
= (((*spos
)[0] & 0x3f) << 8) | (*spos
)[1];
2422 if (offset
>= *spos
- start
) {
2423 wpa_printf(MSG_DEBUG
, "P2P: Invalid DNS "
2424 "pointer offset %u", offset
);
2429 spos_tmp
= start
+ offset
;
2430 return p2p_sd_dns_uncompress_label(upos
, uend
, start
,
2436 len
= (*spos
)[0] & 0x3f;
2441 if (*spos
+ len
> end
) {
2442 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
2443 "sequence - no room for label with length "
2448 if (*upos
+ len
+ 2 > uend
)
2451 os_memcpy(*upos
, *spos
, len
);
2463 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2464 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2465 * not large enough */
2466 static int p2p_sd_dns_uncompress(char *buf
, size_t buf_len
, const u8
*msg
,
2467 size_t msg_len
, size_t offset
)
2469 /* 27-octet in-memory packet from P2P specification */
2470 const char *prefix
= "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2471 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2472 u8
*tmp
, *end
, *spos
;
2478 if (offset
> msg_len
)
2481 tmp
= os_malloc(DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
+ msg_len
);
2484 spos
= tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
;
2485 end
= spos
+ msg_len
;
2488 os_memset(tmp
, 0, DNS_HEADER_LEN
);
2489 os_memcpy(tmp
+ DNS_HEADER_LEN
, prefix
, P2P_SD_IN_MEMORY_LEN
);
2490 os_memcpy(tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
, msg
, msg_len
);
2493 uend
= buf
+ buf_len
;
2495 ret
= p2p_sd_dns_uncompress_label(&upos
, uend
, tmp
, &spos
, end
);
2504 } else if (upos
[-1] == '.')
2512 static struct p2p_srv_bonjour
*
2513 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
2514 const struct wpabuf
*query
)
2516 struct p2p_srv_bonjour
*bsrv
;
2519 len
= wpabuf_len(query
);
2520 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
2521 struct p2p_srv_bonjour
, list
) {
2522 if (len
== wpabuf_len(bsrv
->query
) &&
2523 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
2531 static struct p2p_srv_upnp
*
2532 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2533 const char *service
)
2535 struct p2p_srv_upnp
*usrv
;
2537 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
2538 struct p2p_srv_upnp
, list
) {
2539 if (version
== usrv
->version
&&
2540 os_strcmp(service
, usrv
->service
) == 0)
2547 static void wpas_sd_add_empty(struct wpabuf
*resp
, u8 srv_proto
,
2548 u8 srv_trans_id
, u8 status
)
2552 if (wpabuf_tailroom(resp
) < 5)
2555 /* Length (to be filled) */
2556 len_pos
= wpabuf_put(resp
, 2);
2557 wpabuf_put_u8(resp
, srv_proto
);
2558 wpabuf_put_u8(resp
, srv_trans_id
);
2560 wpabuf_put_u8(resp
, status
);
2561 /* Response Data: empty */
2562 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
2566 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
2569 wpas_sd_add_empty(resp
, srv_proto
, srv_trans_id
,
2570 P2P_SD_PROTO_NOT_AVAILABLE
);
2574 static void wpas_sd_add_bad_request(struct wpabuf
*resp
, u8 srv_proto
,
2577 wpas_sd_add_empty(resp
, srv_proto
, srv_trans_id
, P2P_SD_BAD_REQUEST
);
2581 static void wpas_sd_add_not_found(struct wpabuf
*resp
, u8 srv_proto
,
2584 wpas_sd_add_empty(resp
, srv_proto
, srv_trans_id
,
2585 P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
2589 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
2590 struct wpabuf
*resp
, u8 srv_trans_id
)
2592 struct p2p_srv_bonjour
*bsrv
;
2595 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
2597 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
2598 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
2602 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
2603 struct p2p_srv_bonjour
, list
) {
2604 if (wpabuf_tailroom(resp
) <
2605 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
2607 /* Length (to be filled) */
2608 len_pos
= wpabuf_put(resp
, 2);
2609 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
2610 wpabuf_put_u8(resp
, srv_trans_id
);
2612 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
2613 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
2614 wpabuf_head(bsrv
->resp
),
2615 wpabuf_len(bsrv
->resp
));
2617 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
2618 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
2619 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
2625 static int match_bonjour_query(struct p2p_srv_bonjour
*bsrv
, const u8
*query
,
2628 char str_rx
[256], str_srv
[256];
2630 if (query_len
< 3 || wpabuf_len(bsrv
->query
) < 3)
2631 return 0; /* Too short to include DNS Type and Version */
2632 if (os_memcmp(query
+ query_len
- 3,
2633 wpabuf_head_u8(bsrv
->query
) + wpabuf_len(bsrv
->query
) - 3,
2635 return 0; /* Mismatch in DNS Type or Version */
2636 if (query_len
== wpabuf_len(bsrv
->query
) &&
2637 os_memcmp(query
, wpabuf_head(bsrv
->query
), query_len
- 3) == 0)
2638 return 1; /* Binary match */
2640 if (p2p_sd_dns_uncompress(str_rx
, sizeof(str_rx
), query
, query_len
- 3,
2642 return 0; /* Failed to uncompress query */
2643 if (p2p_sd_dns_uncompress(str_srv
, sizeof(str_srv
),
2644 wpabuf_head(bsrv
->query
),
2645 wpabuf_len(bsrv
->query
) - 3, 0))
2646 return 0; /* Failed to uncompress service */
2648 return os_strcmp(str_rx
, str_srv
) == 0;
2652 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
2653 struct wpabuf
*resp
, u8 srv_trans_id
,
2654 const u8
*query
, size_t query_len
)
2656 struct p2p_srv_bonjour
*bsrv
;
2660 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
2662 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
2663 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
2664 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
2669 if (query_len
== 0) {
2670 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
2674 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
2675 struct p2p_srv_bonjour
, list
) {
2676 if (!match_bonjour_query(bsrv
, query
, query_len
))
2679 if (wpabuf_tailroom(resp
) <
2680 5 + query_len
+ wpabuf_len(bsrv
->resp
))
2685 /* Length (to be filled) */
2686 len_pos
= wpabuf_put(resp
, 2);
2687 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
2688 wpabuf_put_u8(resp
, srv_trans_id
);
2691 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
2692 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
2693 wpabuf_head(bsrv
->resp
),
2694 wpabuf_len(bsrv
->resp
));
2697 wpabuf_put_data(resp
, query
, query_len
); /* Key */
2698 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
2700 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
2704 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
2706 if (wpabuf_tailroom(resp
) < 5)
2709 /* Length (to be filled) */
2710 len_pos
= wpabuf_put(resp
, 2);
2711 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
2712 wpabuf_put_u8(resp
, srv_trans_id
);
2715 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
2716 /* Response Data: empty */
2717 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
2723 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
2724 struct wpabuf
*resp
, u8 srv_trans_id
)
2726 struct p2p_srv_upnp
*usrv
;
2729 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
2731 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
2732 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
2736 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
2737 struct p2p_srv_upnp
, list
) {
2738 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
2741 /* Length (to be filled) */
2742 len_pos
= wpabuf_put(resp
, 2);
2743 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
2744 wpabuf_put_u8(resp
, srv_trans_id
);
2747 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
2749 wpabuf_put_u8(resp
, usrv
->version
);
2750 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
2752 wpabuf_put_str(resp
, usrv
->service
);
2753 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
2759 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
2760 struct wpabuf
*resp
, u8 srv_trans_id
,
2761 const u8
*query
, size_t query_len
)
2763 struct p2p_srv_upnp
*usrv
;
2769 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
2772 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
2773 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
2774 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
2779 if (query_len
== 0) {
2780 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
2784 if (wpabuf_tailroom(resp
) < 5)
2787 /* Length (to be filled) */
2788 len_pos
= wpabuf_put(resp
, 2);
2789 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
2790 wpabuf_put_u8(resp
, srv_trans_id
);
2793 str
= os_malloc(query_len
);
2796 os_memcpy(str
, query
+ 1, query_len
- 1);
2797 str
[query_len
- 1] = '\0';
2799 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
2800 struct p2p_srv_upnp
, list
) {
2801 if (version
!= usrv
->version
)
2804 if (os_strcmp(str
, "ssdp:all") != 0 &&
2805 os_strstr(usrv
->service
, str
) == NULL
)
2808 if (wpabuf_tailroom(resp
) < 2)
2812 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
2814 wpabuf_put_u8(resp
, version
);
2816 wpabuf_put_u8(resp
, ',');
2820 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
2822 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
2824 wpabuf_put_str(resp
, usrv
->service
);
2829 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
2832 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
2833 /* Response Data: empty */
2836 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
2840 #ifdef CONFIG_WIFI_DISPLAY
2841 static void wpas_sd_req_wfd(struct wpa_supplicant
*wpa_s
,
2842 struct wpabuf
*resp
, u8 srv_trans_id
,
2843 const u8
*query
, size_t query_len
)
2849 wpa_hexdump(MSG_DEBUG
, "P2P: SD Request for WFD", query
, query_len
);
2851 if (!wpa_s
->global
->wifi_display
) {
2852 wpa_printf(MSG_DEBUG
, "P2P: WFD protocol not available");
2853 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_WIFI_DISPLAY
,
2858 if (query_len
< 1) {
2859 wpa_printf(MSG_DEBUG
, "P2P: Missing WFD Requested Device "
2864 if (wpabuf_tailroom(resp
) < 5)
2869 wpa_printf(MSG_DEBUG
, "P2P: WSD for device role 0x%x", role
);
2871 /* TODO: role specific handling */
2873 /* Length (to be filled) */
2874 len_pos
= wpabuf_put(resp
, 2);
2875 wpabuf_put_u8(resp
, P2P_SERV_WIFI_DISPLAY
);
2876 wpabuf_put_u8(resp
, srv_trans_id
);
2877 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
); /* Status Code */
2879 while (pos
< query
+ query_len
) {
2880 if (*pos
< MAX_WFD_SUBELEMS
&&
2881 wpa_s
->global
->wfd_subelem
[*pos
] &&
2882 wpabuf_tailroom(resp
) >=
2883 wpabuf_len(wpa_s
->global
->wfd_subelem
[*pos
])) {
2884 wpa_printf(MSG_DEBUG
, "P2P: Add WSD response "
2885 "subelement %u", *pos
);
2886 wpabuf_put_buf(resp
, wpa_s
->global
->wfd_subelem
[*pos
]);
2891 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
2893 #endif /* CONFIG_WIFI_DISPLAY */
2896 static int find_p2ps_substr(struct p2ps_advertisement
*adv_data
,
2897 const u8
*needle
, size_t needle_len
)
2899 const u8
*haystack
= (const u8
*) adv_data
->svc_info
;
2900 size_t haystack_len
, i
;
2902 /* Allow search term to be empty */
2903 if (!needle
|| !needle_len
)
2909 haystack_len
= os_strlen(adv_data
->svc_info
);
2910 for (i
= 0; i
< haystack_len
; i
++) {
2911 if (haystack_len
- i
< needle_len
)
2913 if (os_memcmp(haystack
+ i
, needle
, needle_len
) == 0)
2921 static void wpas_sd_req_asp(struct wpa_supplicant
*wpa_s
,
2922 struct wpabuf
*resp
, u8 srv_trans_id
,
2923 const u8
*query
, size_t query_len
)
2925 struct p2ps_advertisement
*adv_data
;
2926 const u8
*svc
= &query
[1];
2927 const u8
*info
= NULL
;
2928 size_t svc_len
= query
[0];
2929 size_t info_len
= 0;
2931 u8
*count_pos
= NULL
;
2934 wpa_hexdump(MSG_DEBUG
, "P2P: SD Request for ASP", query
, query_len
);
2936 if (!wpa_s
->global
->p2p
) {
2937 wpa_printf(MSG_DEBUG
, "P2P: ASP protocol not available");
2938 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_P2PS
, srv_trans_id
);
2942 /* Info block is optional */
2943 if (svc_len
+ 1 < query_len
) {
2944 info
= &svc
[svc_len
];
2948 /* Range check length of svc string and info block */
2949 if (svc_len
+ (info_len
? info_len
+ 2 : 1) > query_len
) {
2950 wpa_printf(MSG_DEBUG
, "P2P: ASP bad request");
2951 wpas_sd_add_bad_request(resp
, P2P_SERV_P2PS
, srv_trans_id
);
2955 /* Detect and correct for prefix search */
2956 if (svc_len
&& svc
[svc_len
- 1] == '*') {
2961 for (adv_data
= p2p_get_p2ps_adv_list(wpa_s
->global
->p2p
);
2962 adv_data
; adv_data
= adv_data
->next
) {
2963 /* If not a prefix match, reject length mismatches */
2964 if (!prefix
&& svc_len
!= os_strlen(adv_data
->svc_name
))
2967 /* Search each service for request */
2968 if (os_memcmp(adv_data
->svc_name
, svc
, svc_len
) == 0 &&
2969 find_p2ps_substr(adv_data
, info
, info_len
)) {
2970 size_t len
= os_strlen(adv_data
->svc_name
);
2971 size_t svc_info_len
= 0;
2973 if (adv_data
->svc_info
)
2974 svc_info_len
= os_strlen(adv_data
->svc_info
);
2976 if (len
> 0xff || svc_info_len
> 0xffff)
2979 /* Length & Count to be filled as we go */
2980 if (!len_pos
&& !count_pos
) {
2981 if (wpabuf_tailroom(resp
) <
2982 len
+ svc_info_len
+ 16)
2985 len_pos
= wpabuf_put(resp
, 2);
2986 wpabuf_put_u8(resp
, P2P_SERV_P2PS
);
2987 wpabuf_put_u8(resp
, srv_trans_id
);
2989 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
2990 count_pos
= wpabuf_put(resp
, 1);
2992 } else if (wpabuf_tailroom(resp
) <
2993 len
+ svc_info_len
+ 10)
2997 wpa_printf(MSG_DEBUG
,
2998 "P2P: Add Svc: %s info: %s",
3000 adv_data
->svc_info
);
3002 wpa_printf(MSG_DEBUG
, "P2P: Add Svc: %s",
3003 adv_data
->svc_name
);
3006 /* Advertisement ID */
3007 wpabuf_put_le32(resp
, adv_data
->id
);
3009 /* Config Methods */
3010 wpabuf_put_be16(resp
, adv_data
->config_methods
);
3013 wpabuf_put_u8(resp
, (u8
) len
);
3014 wpabuf_put_data(resp
, adv_data
->svc_name
, len
);
3017 wpabuf_put_u8(resp
, adv_data
->state
);
3019 /* Service Information */
3020 wpabuf_put_le16(resp
, (u16
) svc_info_len
);
3021 wpabuf_put_data(resp
, adv_data
->svc_info
, svc_info_len
);
3023 /* Update length and count */
3025 WPA_PUT_LE16(len_pos
,
3026 (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
3030 /* Return error if no matching svc found */
3031 if (count_pos
== NULL
) {
3032 wpa_printf(MSG_DEBUG
, "P2P: ASP service not found");
3033 wpas_sd_add_not_found(resp
, P2P_SERV_P2PS
, srv_trans_id
);
3038 static void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
3039 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
3041 struct wpa_supplicant
*wpa_s
= ctx
;
3042 const u8
*pos
= tlvs
;
3043 const u8
*end
= tlvs
+ tlvs_len
;
3046 struct wpabuf
*resp
;
3047 u8 srv_proto
, srv_trans_id
;
3051 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
3053 buf_len
= 2 * tlvs_len
+ 1;
3054 buf
= os_malloc(buf_len
);
3056 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
3057 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
3059 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
3064 if (wpa_s
->p2p_sd_over_ctrl_iface
) {
3065 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
3066 update_indic
, tlvs
, tlvs_len
);
3067 return; /* to be processed by an external program */
3070 resp
= wpabuf_alloc(10000);
3074 while (pos
+ 1 < end
) {
3075 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
3076 slen
= WPA_GET_LE16(pos
);
3078 if (pos
+ slen
> end
|| slen
< 2) {
3079 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
3084 tlv_end
= pos
+ slen
;
3087 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
3089 srv_trans_id
= *pos
++;
3090 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
3093 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
3094 pos
, tlv_end
- pos
);
3097 if (wpa_s
->force_long_sd
) {
3098 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
3100 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
3101 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
3105 switch (srv_proto
) {
3106 case P2P_SERV_ALL_SERVICES
:
3107 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
3108 "for all services");
3109 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
3110 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
3111 wpa_printf(MSG_DEBUG
, "P2P: No service "
3112 "discovery protocols available");
3113 wpas_sd_add_proto_not_avail(
3114 resp
, P2P_SERV_ALL_SERVICES
,
3118 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
3119 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
3121 case P2P_SERV_BONJOUR
:
3122 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
3123 pos
, tlv_end
- pos
);
3126 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
3127 pos
, tlv_end
- pos
);
3129 #ifdef CONFIG_WIFI_DISPLAY
3130 case P2P_SERV_WIFI_DISPLAY
:
3131 wpas_sd_req_wfd(wpa_s
, resp
, srv_trans_id
,
3132 pos
, tlv_end
- pos
);
3134 #endif /* CONFIG_WIFI_DISPLAY */
3136 wpas_sd_req_asp(wpa_s
, resp
, srv_trans_id
,
3137 pos
, tlv_end
- pos
);
3140 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
3141 "protocol %u", srv_proto
);
3142 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
3151 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
3152 update_indic
, tlvs
, tlvs_len
);
3154 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
3160 static void wpas_sd_p2ps_serv_response(struct wpa_supplicant
*wpa_s
,
3161 const u8
*sa
, u8 srv_trans_id
,
3162 const u8
*pos
, const u8
*tlv_end
)
3170 while (left
-- && pos
< tlv_end
) {
3175 /* Sanity check fixed length+svc_str */
3176 if (pos
+ 6 >= tlv_end
)
3179 if (pos
+ svc_len
+ 10 > tlv_end
)
3182 /* Advertisement ID */
3183 adv_id
= WPA_GET_LE32(pos
);
3186 /* Config Methods */
3187 config_methods
= WPA_GET_BE16(pos
);
3191 pos
++; /* svc_len */
3192 os_memcpy(svc_str
, pos
, svc_len
);
3193 svc_str
[svc_len
] = '\0';
3196 /* Service Status */
3197 svc_status
= *pos
++;
3199 /* Service Information Length */
3200 buf_len
= WPA_GET_LE16(pos
);
3203 /* Sanity check buffer length */
3204 if (buf_len
> (unsigned int) (tlv_end
- pos
))
3208 buf
= os_zalloc(2 * buf_len
+ 1);
3210 utf8_escape((const char *) pos
, buf_len
, buf
,
3218 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_ASP_RESP
3219 MACSTR
" %x %x %x %x %s '%s'",
3220 MAC2STR(sa
), srv_trans_id
, adv_id
,
3221 svc_status
, config_methods
, svc_str
,
3225 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_ASP_RESP
3226 MACSTR
" %x %x %x %x %s",
3227 MAC2STR(sa
), srv_trans_id
, adv_id
,
3228 svc_status
, config_methods
, svc_str
);
3234 static void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
3235 const u8
*tlvs
, size_t tlvs_len
)
3237 struct wpa_supplicant
*wpa_s
= ctx
;
3238 const u8
*pos
= tlvs
;
3239 const u8
*end
= tlvs
+ tlvs_len
;
3245 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
3247 if (tlvs_len
> 1500) {
3248 /* TODO: better way for handling this */
3249 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
3250 P2P_EVENT_SERV_DISC_RESP MACSTR
3251 " %u <long response: %u bytes>",
3252 MAC2STR(sa
), update_indic
,
3253 (unsigned int) tlvs_len
);
3255 buf_len
= 2 * tlvs_len
+ 1;
3256 buf
= os_malloc(buf_len
);
3258 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
3259 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
3260 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
3261 MAC2STR(sa
), update_indic
, buf
);
3267 u8 srv_proto
, srv_trans_id
, status
;
3269 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
3270 slen
= WPA_GET_LE16(pos
);
3272 if (pos
+ slen
> end
|| slen
< 3) {
3273 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
3277 tlv_end
= pos
+ slen
;
3280 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
3282 srv_trans_id
= *pos
++;
3283 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
3286 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
3289 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
3290 pos
, tlv_end
- pos
);
3292 if (srv_proto
== P2P_SERV_P2PS
&& pos
< tlv_end
) {
3293 wpas_sd_p2ps_serv_response(wpa_s
, sa
, srv_trans_id
,
3300 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
3304 u64
wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
3305 const struct wpabuf
*tlvs
)
3307 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3309 return (uintptr_t) p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
3313 u64
wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
3314 u8 version
, const char *query
)
3316 struct wpabuf
*tlvs
;
3319 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
3322 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
3323 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
3324 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
3325 wpabuf_put_u8(tlvs
, version
);
3326 wpabuf_put_str(tlvs
, query
);
3327 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
3333 u64
wpas_p2p_sd_request_asp(struct wpa_supplicant
*wpa_s
, const u8
*dst
, u8 id
,
3334 const char *svc_str
, const char *info_substr
)
3336 struct wpabuf
*tlvs
;
3337 size_t plen
, svc_len
, substr_len
= 0;
3340 svc_len
= os_strlen(svc_str
);
3342 substr_len
= os_strlen(info_substr
);
3344 if (svc_len
> 0xff || substr_len
> 0xff)
3347 plen
= 1 + 1 + 1 + svc_len
+ 1 + substr_len
;
3348 tlvs
= wpabuf_alloc(2 + plen
);
3352 wpabuf_put_le16(tlvs
, plen
);
3353 wpabuf_put_u8(tlvs
, P2P_SERV_P2PS
);
3354 wpabuf_put_u8(tlvs
, id
); /* Service Transaction ID */
3355 wpabuf_put_u8(tlvs
, (u8
) svc_len
); /* Service String Length */
3356 wpabuf_put_data(tlvs
, svc_str
, svc_len
);
3357 wpabuf_put_u8(tlvs
, (u8
) substr_len
); /* Info Substring Length */
3358 wpabuf_put_data(tlvs
, info_substr
, substr_len
);
3359 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
3366 #ifdef CONFIG_WIFI_DISPLAY
3368 static u64
wpas_p2p_sd_request_wfd(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
3369 const struct wpabuf
*tlvs
)
3371 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3373 return (uintptr_t) p2p_sd_request_wfd(wpa_s
->global
->p2p
, dst
, tlvs
);
3377 #define MAX_WFD_SD_SUBELEMS 20
3379 static void wfd_add_sd_req_role(struct wpabuf
*tlvs
, u8 id
, u8 role
,
3380 const char *subelems
)
3387 len
= wpabuf_put(tlvs
, 2);
3388 wpabuf_put_u8(tlvs
, P2P_SERV_WIFI_DISPLAY
); /* Service Protocol Type */
3389 wpabuf_put_u8(tlvs
, id
); /* Service Transaction ID */
3391 wpabuf_put_u8(tlvs
, role
);
3396 if (val
>= 0 && val
< 256) {
3397 wpabuf_put_u8(tlvs
, val
);
3399 if (count
== MAX_WFD_SD_SUBELEMS
)
3402 pos
= os_strchr(pos
+ 1, ',');
3408 WPA_PUT_LE16(len
, (u8
*) wpabuf_put(tlvs
, 0) - len
- 2);
3412 u64
wpas_p2p_sd_request_wifi_display(struct wpa_supplicant
*wpa_s
,
3413 const u8
*dst
, const char *role
)
3415 struct wpabuf
*tlvs
;
3417 const char *subelems
;
3420 subelems
= os_strchr(role
, ' ');
3421 if (subelems
== NULL
)
3425 tlvs
= wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS
));
3429 if (os_strstr(role
, "[source]"))
3430 wfd_add_sd_req_role(tlvs
, id
++, 0x00, subelems
);
3431 if (os_strstr(role
, "[pri-sink]"))
3432 wfd_add_sd_req_role(tlvs
, id
++, 0x01, subelems
);
3433 if (os_strstr(role
, "[sec-sink]"))
3434 wfd_add_sd_req_role(tlvs
, id
++, 0x02, subelems
);
3435 if (os_strstr(role
, "[source+sink]"))
3436 wfd_add_sd_req_role(tlvs
, id
++, 0x03, subelems
);
3438 ret
= wpas_p2p_sd_request_wfd(wpa_s
, dst
, tlvs
);
3443 #endif /* CONFIG_WIFI_DISPLAY */
3446 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, u64 req
)
3448 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3450 return p2p_sd_cancel_request(wpa_s
->global
->p2p
,
3451 (void *) (uintptr_t) req
);
3455 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
3456 const u8
*dst
, u8 dialog_token
,
3457 const struct wpabuf
*resp_tlvs
)
3459 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3461 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
3466 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
3468 if (wpa_s
->global
->p2p
)
3469 p2p_sd_service_update(wpa_s
->global
->p2p
);
3473 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
3475 dl_list_del(&bsrv
->list
);
3476 wpabuf_free(bsrv
->query
);
3477 wpabuf_free(bsrv
->resp
);
3482 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
3484 dl_list_del(&usrv
->list
);
3485 os_free(usrv
->service
);
3490 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
3492 struct p2p_srv_bonjour
*bsrv
, *bn
;
3493 struct p2p_srv_upnp
*usrv
, *un
;
3495 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
3496 struct p2p_srv_bonjour
, list
)
3497 wpas_p2p_srv_bonjour_free(bsrv
);
3499 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
3500 struct p2p_srv_upnp
, list
)
3501 wpas_p2p_srv_upnp_free(usrv
);
3503 wpas_p2p_sd_service_update(wpa_s
);
3507 int wpas_p2p_service_p2ps_id_exists(struct wpa_supplicant
*wpa_s
, u32 adv_id
)
3512 if (p2p_service_p2ps_id(wpa_s
->global
->p2p
, adv_id
))
3519 int wpas_p2p_service_del_asp(struct wpa_supplicant
*wpa_s
, u32 adv_id
)
3521 return p2p_service_del_asp(wpa_s
->global
->p2p
, adv_id
);
3525 int wpas_p2p_service_add_asp(struct wpa_supplicant
*wpa_s
,
3526 int auto_accept
, u32 adv_id
,
3527 const char *adv_str
, u8 svc_state
,
3528 u16 config_methods
, const char *svc_info
)
3530 return p2p_service_add_asp(wpa_s
->global
->p2p
, auto_accept
, adv_id
,
3531 adv_str
, svc_state
, config_methods
,
3536 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
3537 struct wpabuf
*query
, struct wpabuf
*resp
)
3539 struct p2p_srv_bonjour
*bsrv
;
3541 bsrv
= os_zalloc(sizeof(*bsrv
));
3544 bsrv
->query
= query
;
3546 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
3548 wpas_p2p_sd_service_update(wpa_s
);
3553 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
3554 const struct wpabuf
*query
)
3556 struct p2p_srv_bonjour
*bsrv
;
3558 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
3561 wpas_p2p_srv_bonjour_free(bsrv
);
3562 wpas_p2p_sd_service_update(wpa_s
);
3567 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
3568 const char *service
)
3570 struct p2p_srv_upnp
*usrv
;
3572 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
3573 return 0; /* Already listed */
3574 usrv
= os_zalloc(sizeof(*usrv
));
3577 usrv
->version
= version
;
3578 usrv
->service
= os_strdup(service
);
3579 if (usrv
->service
== NULL
) {
3583 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
3585 wpas_p2p_sd_service_update(wpa_s
);
3590 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
3591 const char *service
)
3593 struct p2p_srv_upnp
*usrv
;
3595 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
3598 wpas_p2p_srv_upnp_free(usrv
);
3599 wpas_p2p_sd_service_update(wpa_s
);
3604 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
3605 const u8
*peer
, const char *params
,
3606 unsigned int generated_pin
)
3608 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
3609 " %08d%s", MAC2STR(peer
), generated_pin
, params
);
3613 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
3614 const u8
*peer
, const char *params
)
3616 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
3617 "%s", MAC2STR(peer
), params
);
3621 static void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
3622 const u8
*dev_addr
, const u8
*pri_dev_type
,
3623 const char *dev_name
, u16 supp_config_methods
,
3624 u8 dev_capab
, u8 group_capab
, const u8
*group_id
,
3625 size_t group_id_len
)
3627 struct wpa_supplicant
*wpa_s
= ctx
;
3628 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
3630 u8 empty_dev_type
[8];
3631 unsigned int generated_pin
= 0;
3632 struct wpa_supplicant
*group
= NULL
;
3636 for (group
= wpa_s
->global
->ifaces
; group
; group
= group
->next
)
3638 struct wpa_ssid
*s
= group
->current_ssid
;
3640 s
->mode
== WPAS_MODE_P2P_GO
&&
3641 group_id_len
- ETH_ALEN
== s
->ssid_len
&&
3642 os_memcmp(group_id
+ ETH_ALEN
, s
->ssid
,
3648 if (pri_dev_type
== NULL
) {
3649 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
3650 pri_dev_type
= empty_dev_type
;
3652 res
= os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
3653 " pri_dev_type=%s name='%s' config_methods=0x%x "
3654 "dev_capab=0x%x group_capab=0x%x%s%s",
3656 wps_dev_type_bin2str(pri_dev_type
, devtype
,
3658 dev_name
, supp_config_methods
, dev_capab
, group_capab
,
3659 group
? " group=" : "",
3660 group
? group
->ifname
: "");
3661 if (os_snprintf_error(sizeof(params
), res
))
3662 wpa_printf(MSG_DEBUG
, "P2P: PD Request event truncated");
3663 params
[sizeof(params
) - 1] = '\0';
3665 if (config_methods
& WPS_CONFIG_DISPLAY
) {
3666 generated_pin
= wps_generate_pin();
3667 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
3669 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
3670 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
3671 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
3672 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ
3673 MACSTR
"%s", MAC2STR(peer
), params
);
3675 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 1 /* request */,
3676 P2P_PROV_DISC_SUCCESS
,
3677 config_methods
, generated_pin
);
3681 static void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
3683 struct wpa_supplicant
*wpa_s
= ctx
;
3684 unsigned int generated_pin
= 0;
3687 if (wpa_s
->pending_pd_before_join
&&
3688 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
3689 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
3690 wpa_s
->pending_pd_before_join
= 0;
3691 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
3692 "join-existing-group operation");
3693 wpas_p2p_join_start(wpa_s
, 0, NULL
, 0);
3697 if (wpa_s
->pending_pd_use
== AUTO_PD_JOIN
||
3698 wpa_s
->pending_pd_use
== AUTO_PD_GO_NEG
) {
3701 res
= os_snprintf(params
, sizeof(params
), " peer_go=%d",
3702 wpa_s
->pending_pd_use
== AUTO_PD_JOIN
);
3703 if (os_snprintf_error(sizeof(params
), res
))
3704 params
[sizeof(params
) - 1] = '\0';
3708 if (config_methods
& WPS_CONFIG_DISPLAY
)
3709 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
3710 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
3711 generated_pin
= wps_generate_pin();
3712 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
3714 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
3715 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP
3716 MACSTR
"%s", MAC2STR(peer
), params
);
3718 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
3719 P2P_PROV_DISC_SUCCESS
,
3720 config_methods
, generated_pin
);
3724 static void wpas_prov_disc_fail(void *ctx
, const u8
*peer
,
3725 enum p2p_prov_disc_status status
,
3726 u32 adv_id
, const u8
*adv_mac
,
3727 const char *deferred_session_resp
)
3729 struct wpa_supplicant
*wpa_s
= ctx
;
3731 if (wpa_s
->p2p_fallback_to_go_neg
) {
3732 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: PD for p2p_connect-auto "
3733 "failed - fall back to GO Negotiation");
3734 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
3735 P2P_EVENT_FALLBACK_TO_GO_NEG
3736 "reason=PD-failed");
3737 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
3741 if (status
== P2P_PROV_DISC_TIMEOUT_JOIN
) {
3742 wpa_s
->pending_pd_before_join
= 0;
3743 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
3744 "join-existing-group operation (no ACK for PD "
3746 wpas_p2p_join_start(wpa_s
, 0, NULL
, 0);
3750 if (adv_id
&& adv_mac
&& deferred_session_resp
) {
3751 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
3752 " p2p_dev_addr=" MACSTR
" status=%d adv_id=%x"
3753 " deferred_session_resp='%s'",
3754 MAC2STR(peer
), status
, adv_id
,
3755 deferred_session_resp
);
3756 } else if (adv_id
&& adv_mac
) {
3757 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
3758 " p2p_dev_addr=" MACSTR
" status=%d adv_id=%x",
3759 MAC2STR(peer
), status
, adv_id
);
3761 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
3762 " p2p_dev_addr=" MACSTR
" status=%d",
3763 MAC2STR(peer
), status
);
3766 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
3771 static int freq_included(const struct p2p_channels
*channels
, unsigned int freq
)
3773 if (channels
== NULL
)
3774 return 1; /* Assume no restrictions */
3775 return p2p_channels_includes_freq(channels
, freq
);
3781 * Pick the best frequency to use from all the currently used frequencies.
3783 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant
*wpa_s
,
3784 struct wpa_used_freq_data
*freqs
,
3789 /* find a candidate freq that is supported by P2P */
3790 for (c
= 0; c
< num
; c
++)
3791 if (p2p_supported_freq(wpa_s
->global
->p2p
, freqs
[c
].freq
))
3797 /* once we have a candidate, try to find a 'better' one */
3798 for (i
= c
+ 1; i
< num
; i
++) {
3799 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freqs
[i
].freq
))
3803 * 1. Infrastructure station interfaces have higher preference.
3804 * 2. P2P Clients have higher preference.
3807 if (freqs
[i
].flags
& WPA_FREQ_USED_BY_INFRA_STATION
) {
3812 if ((freqs
[i
].flags
& WPA_FREQ_USED_BY_P2P_CLIENT
))
3815 return freqs
[c
].freq
;
3819 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
3820 const u8
*go_dev_addr
, const u8
*ssid
,
3821 size_t ssid_len
, int *go
, u8
*group_bssid
,
3822 int *force_freq
, int persistent_group
,
3823 const struct p2p_channels
*channels
,
3826 struct wpa_supplicant
*wpa_s
= ctx
;
3828 struct wpa_used_freq_data
*freqs
;
3829 struct wpa_supplicant
*grp
;
3832 if (!persistent_group
) {
3833 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
3834 " to join an active group (SSID: %s)",
3835 MAC2STR(sa
), wpa_ssid_txt(ssid
, ssid_len
));
3836 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
3837 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
3839 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
3840 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
3841 "authorized invitation");
3845 #ifdef CONFIG_WPS_NFC
3846 if (dev_pw_id
>= 0 && wpa_s
->p2p_nfc_tag_enabled
&&
3847 dev_pw_id
== wpa_s
->p2p_oob_dev_pw_id
) {
3848 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation based on local enabled NFC Tag");
3849 wpa_s
->p2p_wps_method
= WPS_NFC
;
3850 wpa_s
->pending_join_wps_method
= WPS_NFC
;
3851 os_memcpy(wpa_s
->pending_join_dev_addr
,
3852 go_dev_addr
, ETH_ALEN
);
3853 os_memcpy(wpa_s
->pending_join_iface_addr
,
3857 #endif /* CONFIG_WPS_NFC */
3860 * Do not accept the invitation automatically; notify user and
3863 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
3866 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
3868 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
3869 "running persistent group");
3871 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
3875 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
3876 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0) {
3877 wpa_printf(MSG_DEBUG
, "P2P: Accept previously initiated "
3878 "invitation to re-invoke a persistent group");
3879 os_memset(wpa_s
->p2p_auth_invite
, 0, ETH_ALEN
);
3880 } else if (!wpa_s
->conf
->persistent_reconnect
)
3881 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
3883 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
3884 if (s
->disabled
== 2 &&
3885 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
3886 s
->ssid_len
== ssid_len
&&
3887 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
3892 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
3893 " requested reinvocation of an unknown group",
3895 return P2P_SC_FAIL_UNKNOWN_GROUP
;
3898 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
3900 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
3901 wpa_printf(MSG_DEBUG
, "P2P: The only available "
3902 "interface is already in use - reject "
3904 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
3906 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
3907 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
3909 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
3911 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
3912 "interface address for the group");
3913 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
3915 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
3920 wpas_p2p_set_own_freq_preference(wpa_s
, 0);
3923 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
,
3924 sizeof(struct wpa_used_freq_data
));
3926 int num_channels
= wpa_s
->num_multichan_concurrent
;
3927 int num
= wpas_p2p_valid_oper_freqs(wpa_s
, freqs
, num_channels
);
3928 best_freq
= wpas_p2p_pick_best_used_freq(wpa_s
, freqs
, num
);
3932 /* Get one of the frequencies currently in use */
3933 if (best_freq
> 0) {
3934 wpa_printf(MSG_DEBUG
, "P2P: Trying to prefer a channel already used by one of the interfaces");
3935 wpas_p2p_set_own_freq_preference(wpa_s
, best_freq
);
3937 if (wpa_s
->num_multichan_concurrent
< 2 ||
3938 wpas_p2p_num_unused_channels(wpa_s
) < 1) {
3939 wpa_printf(MSG_DEBUG
, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3940 *force_freq
= best_freq
;
3944 if (*force_freq
> 0 && wpa_s
->num_multichan_concurrent
> 1 &&
3945 wpas_p2p_num_unused_channels(wpa_s
) > 0) {
3947 /* We are the client */
3948 wpa_printf(MSG_DEBUG
, "P2P: Peer was found to be "
3949 "running a GO but we are capable of MCC, "
3950 "figure out the best channel to use");
3952 } else if (!freq_included(channels
, *force_freq
)) {
3953 /* We are the GO, and *force_freq is not in the
3955 wpa_printf(MSG_DEBUG
, "P2P: Forced GO freq %d MHz not "
3956 "in intersection but we are capable of MCC, "
3957 "figure out the best channel to use",
3963 return P2P_SC_SUCCESS
;
3967 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
3968 const u8
*ssid
, size_t ssid_len
,
3969 const u8
*go_dev_addr
, u8 status
,
3972 struct wpa_supplicant
*wpa_s
= ctx
;
3975 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
3976 if (s
->disabled
== 2 &&
3977 s
->ssid_len
== ssid_len
&&
3978 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
3982 if (status
== P2P_SC_SUCCESS
) {
3983 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
3984 " was accepted; op_freq=%d MHz, SSID=%s",
3985 MAC2STR(sa
), op_freq
, wpa_ssid_txt(ssid
, ssid_len
));
3987 int go
= s
->mode
== WPAS_MODE_P2P_GO
;
3988 wpas_p2p_group_add_persistent(
3989 wpa_s
, s
, go
, 0, op_freq
, 0, 0, NULL
,
3990 go
? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
: 0);
3992 wpa_s
->user_initiated_pd
= 0;
3993 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
3994 wpa_s
->p2p_wps_method
, 0, op_freq
,
4000 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
4001 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
4002 " was rejected (status %u)", MAC2STR(sa
), status
);
4008 wpa_msg_global(wpa_s
, MSG_INFO
,
4009 P2P_EVENT_INVITATION_RECEIVED
4010 "sa=" MACSTR
" go_dev_addr=" MACSTR
4011 " bssid=" MACSTR
" unknown-network",
4012 MAC2STR(sa
), MAC2STR(go_dev_addr
),
4015 wpa_msg_global(wpa_s
, MSG_INFO
,
4016 P2P_EVENT_INVITATION_RECEIVED
4017 "sa=" MACSTR
" go_dev_addr=" MACSTR
4019 MAC2STR(sa
), MAC2STR(go_dev_addr
));
4024 if (s
->mode
== WPAS_MODE_P2P_GO
&& op_freq
) {
4025 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
4026 "sa=" MACSTR
" persistent=%d freq=%d",
4027 MAC2STR(sa
), s
->id
, op_freq
);
4029 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
4030 "sa=" MACSTR
" persistent=%d",
4031 MAC2STR(sa
), s
->id
);
4036 static void wpas_remove_persistent_peer(struct wpa_supplicant
*wpa_s
,
4037 struct wpa_ssid
*ssid
,
4038 const u8
*peer
, int inv
)
4045 for (i
= 0; ssid
->p2p_client_list
&& i
< ssid
->num_p2p_clients
; i
++) {
4046 if (os_memcmp(ssid
->p2p_client_list
+ i
* 2 * ETH_ALEN
, peer
,
4050 if (i
>= ssid
->num_p2p_clients
|| !ssid
->p2p_client_list
) {
4051 if (ssid
->mode
!= WPAS_MODE_P2P_GO
&&
4052 os_memcmp(ssid
->bssid
, peer
, ETH_ALEN
) == 0) {
4053 wpa_printf(MSG_DEBUG
, "P2P: Remove persistent group %d "
4054 "due to invitation result", ssid
->id
);
4055 wpas_notify_network_removed(wpa_s
, ssid
);
4056 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
4059 return; /* Peer not found in client list */
4062 wpa_printf(MSG_DEBUG
, "P2P: Remove peer " MACSTR
" from persistent "
4063 "group %d client list%s",
4064 MAC2STR(peer
), ssid
->id
,
4065 inv
? " due to invitation result" : "");
4066 os_memmove(ssid
->p2p_client_list
+ i
* 2 * ETH_ALEN
,
4067 ssid
->p2p_client_list
+ (i
+ 1) * 2 * ETH_ALEN
,
4068 (ssid
->num_p2p_clients
- i
- 1) * 2 * ETH_ALEN
);
4069 ssid
->num_p2p_clients
--;
4070 if (wpa_s
->parent
->conf
->update_config
&&
4071 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
4072 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
4076 static void wpas_remove_persistent_client(struct wpa_supplicant
*wpa_s
,
4079 struct wpa_ssid
*ssid
;
4081 wpa_s
= wpa_s
->global
->p2p_invite_group
;
4083 return; /* No known invitation group */
4084 ssid
= wpa_s
->current_ssid
;
4085 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
4086 !ssid
->p2p_persistent_group
)
4087 return; /* Not operating as a GO in persistent group */
4088 ssid
= wpas_p2p_get_persistent(wpa_s
->parent
, peer
,
4089 ssid
->ssid
, ssid
->ssid_len
);
4090 wpas_remove_persistent_peer(wpa_s
, ssid
, peer
, 1);
4094 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
,
4095 const struct p2p_channels
*channels
,
4096 const u8
*peer
, int neg_freq
,
4099 struct wpa_supplicant
*wpa_s
= ctx
;
4100 struct wpa_ssid
*ssid
;
4104 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
4105 "status=%d " MACSTR
,
4106 status
, MAC2STR(bssid
));
4108 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
4109 "status=%d ", status
);
4111 wpas_notify_p2p_invitation_result(wpa_s
, status
, bssid
);
4113 wpa_printf(MSG_DEBUG
, "P2P: Invitation result - status=%d peer=" MACSTR
,
4114 status
, MAC2STR(peer
));
4115 if (wpa_s
->pending_invite_ssid_id
== -1) {
4116 if (status
== P2P_SC_FAIL_UNKNOWN_GROUP
)
4117 wpas_remove_persistent_client(wpa_s
, peer
);
4118 return; /* Invitation to active group */
4121 if (status
== P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
4122 wpa_printf(MSG_DEBUG
, "P2P: Waiting for peer to start another "
4123 "invitation exchange to indicate readiness for "
4127 if (status
!= P2P_SC_SUCCESS
) {
4128 if (status
== P2P_SC_FAIL_UNKNOWN_GROUP
) {
4129 ssid
= wpa_config_get_network(
4130 wpa_s
->conf
, wpa_s
->pending_invite_ssid_id
);
4131 wpas_remove_persistent_peer(wpa_s
, ssid
, peer
, 1);
4133 wpas_p2p_remove_pending_group_interface(wpa_s
);
4137 ssid
= wpa_config_get_network(wpa_s
->conf
,
4138 wpa_s
->pending_invite_ssid_id
);
4140 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
4141 "data matching with invitation");
4146 * The peer could have missed our ctrl::ack frame for Invitation
4147 * Response and continue retransmitting the frame. To reduce the
4148 * likelihood of the peer not getting successful TX status for the
4149 * Invitation Response frame, wait a short time here before starting
4150 * the persistent group so that we will remain on the current channel to
4151 * acknowledge any possible retransmission from the peer.
4153 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: 50 ms wait on current channel before "
4154 "starting persistent group");
4157 if (neg_freq
> 0 && ssid
->mode
== WPAS_MODE_P2P_GO
&&
4158 freq_included(channels
, neg_freq
))
4160 else if (peer_oper_freq
> 0 && ssid
->mode
!= WPAS_MODE_P2P_GO
&&
4161 freq_included(channels
, peer_oper_freq
))
4162 freq
= peer_oper_freq
;
4166 wpa_printf(MSG_DEBUG
, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
4167 freq
, wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
4168 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
4169 ssid
->mode
== WPAS_MODE_P2P_GO
,
4170 wpa_s
->p2p_persistent_go_freq
,
4172 wpa_s
->p2p_go_ht40
, wpa_s
->p2p_go_vht
,
4174 ssid
->mode
== WPAS_MODE_P2P_GO
?
4175 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
:
4180 static int wpas_p2p_disallowed_freq(struct wpa_global
*global
,
4183 if (freq_range_list_includes(&global
->p2p_go_avoid_freq
, freq
))
4185 return freq_range_list_includes(&global
->p2p_disallow_freq
, freq
);
4189 static void wpas_p2p_add_chan(struct p2p_reg_class
*reg
, u8 chan
)
4191 reg
->channel
[reg
->channels
] = chan
;
4196 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
4197 struct p2p_channels
*chan
,
4198 struct p2p_channels
*cli_chan
)
4202 wpa_s
->global
->p2p_24ghz_social_channels
= 1;
4204 os_memset(cli_chan
, 0, sizeof(*cli_chan
));
4206 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
4209 /* Operating class 81 - 2.4 GHz band channels 1..13 */
4210 chan
->reg_class
[cla
].reg_class
= 81;
4211 chan
->reg_class
[cla
].channels
= 0;
4212 for (i
= 0; i
< 11; i
++) {
4213 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 2412 + i
* 5))
4214 wpas_p2p_add_chan(&chan
->reg_class
[cla
], i
+ 1);
4216 if (chan
->reg_class
[cla
].channels
)
4219 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
4222 /* Operating class 115 - 5 GHz, channels 36-48 */
4223 chan
->reg_class
[cla
].reg_class
= 115;
4224 chan
->reg_class
[cla
].channels
= 0;
4225 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 36 * 5))
4226 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 36);
4227 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 40 * 5))
4228 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 40);
4229 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 44 * 5))
4230 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 44);
4231 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 48 * 5))
4232 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 48);
4233 if (chan
->reg_class
[cla
].channels
)
4236 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
4239 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
4240 chan
->reg_class
[cla
].reg_class
= 124;
4241 chan
->reg_class
[cla
].channels
= 0;
4242 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 149 * 5))
4243 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 149);
4244 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 153 * 5))
4245 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 153);
4246 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 156 * 5))
4247 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 157);
4248 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 161 * 5))
4249 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 161);
4250 if (chan
->reg_class
[cla
].channels
)
4253 chan
->reg_classes
= cla
;
4258 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
4260 enum hostapd_hw_mode mode
)
4264 for (i
= 0; i
< num_modes
; i
++) {
4265 if (modes
[i
].mode
== mode
)
4274 NOT_ALLOWED
, NO_IR
, ALLOWED
4277 static int has_channel(struct wpa_global
*global
,
4278 struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
4283 freq
= (mode
->mode
== HOSTAPD_MODE_IEEE80211A
? 5000 : 2407) +
4285 if (wpas_p2p_disallowed_freq(global
, freq
))
4288 for (i
= 0; i
< mode
->num_channels
; i
++) {
4289 if (mode
->channels
[i
].chan
== chan
) {
4291 *flags
= mode
->channels
[i
].flag
;
4292 if (mode
->channels
[i
].flag
&
4293 (HOSTAPD_CHAN_DISABLED
|
4294 HOSTAPD_CHAN_RADAR
))
4296 if (mode
->channels
[i
].flag
& HOSTAPD_CHAN_NO_IR
)
4306 struct p2p_oper_class_map
{
4307 enum hostapd_hw_mode mode
;
4312 enum { BW20
, BW40PLUS
, BW40MINUS
, BW80
, BW2160
} bw
;
4315 static struct p2p_oper_class_map op_class
[] = {
4316 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
4317 #if 0 /* Do not enable HT40 on 2 GHz for now */
4318 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
4319 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
4321 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
4322 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
4323 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
4324 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
4325 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
4326 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
4329 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
4330 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
4331 * 80 MHz, but currently use the following definition for simplicity
4332 * (these center frequencies are not actual channels, which makes
4333 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
4334 * removing invalid channels.
4336 { HOSTAPD_MODE_IEEE80211A
, 128, 36, 161, 4, BW80
},
4337 { HOSTAPD_MODE_IEEE80211AD
, 180, 1, 4, 1, BW2160
},
4338 { -1, 0, 0, 0, 0, BW20
}
4342 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant
*wpa_s
,
4343 struct hostapd_hw_modes
*mode
,
4346 u8 center_channels
[] = { 42, 58, 106, 122, 138, 155 };
4349 if (mode
->mode
!= HOSTAPD_MODE_IEEE80211A
)
4352 for (i
= 0; i
< ARRAY_SIZE(center_channels
); i
++)
4354 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
4355 * so the center channel is 6 channels away from the start/end.
4357 if (channel
>= center_channels
[i
] - 6 &&
4358 channel
<= center_channels
[i
] + 6)
4359 return center_channels
[i
];
4365 static enum chan_allowed
wpas_p2p_verify_80mhz(struct wpa_supplicant
*wpa_s
,
4366 struct hostapd_hw_modes
*mode
,
4371 enum chan_allowed res
, ret
= ALLOWED
;
4373 center_chan
= wpas_p2p_get_center_80mhz(wpa_s
, mode
, channel
);
4376 if (center_chan
>= 58 && center_chan
<= 138)
4377 return NOT_ALLOWED
; /* Do not allow DFS channels for P2P */
4379 /* check all the channels are available */
4380 for (i
= 0; i
< 4; i
++) {
4381 int adj_chan
= center_chan
- 6 + i
* 4;
4383 res
= has_channel(wpa_s
->global
, mode
, adj_chan
, &flags
);
4384 if (res
== NOT_ALLOWED
)
4389 if (i
== 0 && !(flags
& HOSTAPD_CHAN_VHT_10_70
))
4391 if (i
== 1 && !(flags
& HOSTAPD_CHAN_VHT_30_50
))
4393 if (i
== 2 && !(flags
& HOSTAPD_CHAN_VHT_50_30
))
4395 if (i
== 3 && !(flags
& HOSTAPD_CHAN_VHT_70_10
))
4403 static enum chan_allowed
wpas_p2p_verify_channel(struct wpa_supplicant
*wpa_s
,
4404 struct hostapd_hw_modes
*mode
,
4408 enum chan_allowed res
, res2
;
4410 res2
= res
= has_channel(wpa_s
->global
, mode
, channel
, &flag
);
4411 if (bw
== BW40MINUS
) {
4412 if (!(flag
& HOSTAPD_CHAN_HT40MINUS
))
4414 res2
= has_channel(wpa_s
->global
, mode
, channel
- 4, NULL
);
4415 } else if (bw
== BW40PLUS
) {
4416 if (!(flag
& HOSTAPD_CHAN_HT40PLUS
))
4418 res2
= has_channel(wpa_s
->global
, mode
, channel
+ 4, NULL
);
4419 } else if (bw
== BW80
) {
4420 res2
= wpas_p2p_verify_80mhz(wpa_s
, mode
, channel
, bw
);
4423 if (res
== NOT_ALLOWED
|| res2
== NOT_ALLOWED
)
4425 if (res
== NO_IR
|| res2
== NO_IR
)
4431 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
4432 struct p2p_channels
*chan
,
4433 struct p2p_channels
*cli_chan
)
4435 struct hostapd_hw_modes
*mode
;
4436 int cla
, op
, cli_cla
;
4438 if (wpa_s
->hw
.modes
== NULL
) {
4439 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
4440 "of all supported channels; assume dualband "
4442 return wpas_p2p_default_channels(wpa_s
, chan
, cli_chan
);
4447 for (op
= 0; op_class
[op
].op_class
; op
++) {
4448 struct p2p_oper_class_map
*o
= &op_class
[op
];
4450 struct p2p_reg_class
*reg
= NULL
, *cli_reg
= NULL
;
4452 mode
= get_mode(wpa_s
->hw
.modes
, wpa_s
->hw
.num_modes
, o
->mode
);
4455 if (mode
->mode
== HOSTAPD_MODE_IEEE80211G
)
4456 wpa_s
->global
->p2p_24ghz_social_channels
= 1;
4457 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
4458 enum chan_allowed res
;
4459 res
= wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
);
4460 if (res
== ALLOWED
) {
4462 wpa_printf(MSG_DEBUG
, "P2P: Add operating class %u",
4464 reg
= &chan
->reg_class
[cla
];
4466 reg
->reg_class
= o
->op_class
;
4468 reg
->channel
[reg
->channels
] = ch
;
4470 } else if (res
== NO_IR
&&
4471 wpa_s
->conf
->p2p_add_cli_chan
) {
4472 if (cli_reg
== NULL
) {
4473 wpa_printf(MSG_DEBUG
, "P2P: Add operating class %u (client only)",
4475 cli_reg
= &cli_chan
->reg_class
[cli_cla
];
4477 cli_reg
->reg_class
= o
->op_class
;
4479 cli_reg
->channel
[cli_reg
->channels
] = ch
;
4480 cli_reg
->channels
++;
4484 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
4485 reg
->channel
, reg
->channels
);
4488 wpa_hexdump(MSG_DEBUG
, "P2P: Channels (client only)",
4489 cli_reg
->channel
, cli_reg
->channels
);
4493 chan
->reg_classes
= cla
;
4494 cli_chan
->reg_classes
= cli_cla
;
4500 int wpas_p2p_get_ht40_mode(struct wpa_supplicant
*wpa_s
,
4501 struct hostapd_hw_modes
*mode
, u8 channel
)
4504 enum chan_allowed ret
;
4506 for (op
= 0; op_class
[op
].op_class
; op
++) {
4507 struct p2p_oper_class_map
*o
= &op_class
[op
];
4510 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
4511 if (o
->mode
!= HOSTAPD_MODE_IEEE80211A
||
4512 o
->bw
== BW20
|| ch
!= channel
)
4514 ret
= wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
);
4516 return (o
->bw
== BW40MINUS
) ? -1 : 1;
4523 int wpas_p2p_get_vht80_center(struct wpa_supplicant
*wpa_s
,
4524 struct hostapd_hw_modes
*mode
, u8 channel
)
4526 if (!wpas_p2p_verify_channel(wpa_s
, mode
, channel
, BW80
))
4529 return wpas_p2p_get_center_80mhz(wpa_s
, mode
, channel
);
4533 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
4536 struct wpa_supplicant
*wpa_s
= ctx
;
4538 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4539 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
4545 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
4549 struct wpa_supplicant
* wpas_get_p2p_go_iface(struct wpa_supplicant
*wpa_s
,
4550 const u8
*ssid
, size_t ssid_len
)
4552 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4553 struct wpa_ssid
*s
= wpa_s
->current_ssid
;
4556 if (s
->mode
!= WPAS_MODE_P2P_GO
&&
4557 s
->mode
!= WPAS_MODE_AP
&&
4558 s
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
)
4560 if (s
->ssid_len
!= ssid_len
||
4561 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
4571 struct wpa_supplicant
* wpas_get_p2p_client_iface(struct wpa_supplicant
*wpa_s
,
4572 const u8
*peer_dev_addr
)
4574 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4575 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
4578 if (ssid
->mode
!= WPAS_MODE_INFRA
)
4580 if (wpa_s
->wpa_state
!= WPA_COMPLETED
&&
4581 wpa_s
->wpa_state
!= WPA_GROUP_HANDSHAKE
)
4583 if (os_memcmp(wpa_s
->go_dev_addr
, peer_dev_addr
, ETH_ALEN
) == 0)
4591 static int wpas_go_connected(void *ctx
, const u8
*dev_addr
)
4593 struct wpa_supplicant
*wpa_s
= ctx
;
4595 return wpas_get_p2p_client_iface(wpa_s
, dev_addr
) != NULL
;
4599 static int wpas_is_concurrent_session_active(void *ctx
)
4601 struct wpa_supplicant
*wpa_s
= ctx
;
4602 struct wpa_supplicant
*ifs
;
4604 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
4607 if (ifs
->wpa_state
> WPA_ASSOCIATED
)
4614 static void wpas_p2p_debug_print(void *ctx
, int level
, const char *msg
)
4616 struct wpa_supplicant
*wpa_s
= ctx
;
4617 wpa_msg_global(wpa_s
, level
, "P2P: %s", msg
);
4621 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant
*wpa_s
,
4622 const char *conf_p2p_dev
)
4624 struct wpa_interface iface
;
4625 struct wpa_supplicant
*p2pdev_wpa_s
;
4627 char force_name
[100];
4630 ret
= os_snprintf(ifname
, sizeof(ifname
), P2P_MGMT_DEVICE_PREFIX
"%s",
4632 if (os_snprintf_error(sizeof(ifname
), ret
))
4634 force_name
[0] = '\0';
4635 wpa_s
->pending_interface_type
= WPA_IF_P2P_DEVICE
;
4636 ret
= wpa_drv_if_add(wpa_s
, WPA_IF_P2P_DEVICE
, ifname
, NULL
, NULL
,
4637 force_name
, wpa_s
->pending_interface_addr
, NULL
);
4639 wpa_printf(MSG_DEBUG
, "P2P: Failed to create P2P Device interface");
4642 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
4643 sizeof(wpa_s
->pending_interface_name
));
4645 os_memset(&iface
, 0, sizeof(iface
));
4647 iface
.ifname
= wpa_s
->pending_interface_name
;
4648 iface
.driver
= wpa_s
->driver
->name
;
4649 iface
.driver_param
= wpa_s
->conf
->driver_param
;
4652 * If a P2P Device configuration file was given, use it as the interface
4653 * configuration file (instead of using parent's configuration file.
4656 iface
.confname
= conf_p2p_dev
;
4657 iface
.ctrl_interface
= NULL
;
4659 iface
.confname
= wpa_s
->confname
;
4660 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
4662 iface
.conf_p2p_dev
= NULL
;
4664 p2pdev_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
4665 if (!p2pdev_wpa_s
) {
4666 wpa_printf(MSG_DEBUG
, "P2P: Failed to add P2P Device interface");
4669 p2pdev_wpa_s
->parent
= wpa_s
;
4670 wpa_s
->p2p_dev
= p2pdev_wpa_s
;
4672 wpa_s
->pending_interface_name
[0] = '\0';
4677 static void wpas_presence_resp(void *ctx
, const u8
*src
, u8 status
,
4678 const u8
*noa
, size_t noa_len
)
4680 struct wpa_supplicant
*wpa_s
, *intf
= ctx
;
4683 for (wpa_s
= intf
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4684 if (wpa_s
->waiting_presence_resp
)
4688 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No group interface was waiting for presence response");
4691 wpa_s
->waiting_presence_resp
= 0;
4693 wpa_snprintf_hex(hex
, sizeof(hex
), noa
, noa_len
);
4694 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PRESENCE_RESPONSE
"src=" MACSTR
4695 " status=%u noa=%s", MAC2STR(src
), status
, hex
);
4699 static int wpas_get_persistent_group(void *ctx
, const u8
*addr
, const u8
*ssid
,
4700 size_t ssid_len
, u8
*go_dev_addr
,
4701 u8
*ret_ssid
, size_t *ret_ssid_len
)
4703 struct wpa_supplicant
*wpa_s
= ctx
;
4706 s
= wpas_p2p_get_persistent(wpa_s
, addr
, ssid
, ssid_len
);
4708 os_memcpy(ret_ssid
, s
->ssid
, s
->ssid_len
);
4709 *ret_ssid_len
= s
->ssid_len
;
4710 os_memcpy(go_dev_addr
, s
->bssid
, ETH_ALEN
);
4718 static int wpas_get_go_info(void *ctx
, u8
*intended_addr
,
4719 u8
*ssid
, size_t *ssid_len
, int *group_iface
)
4721 struct wpa_supplicant
*wpa_s
= ctx
;
4725 s
= wpas_p2p_group_go_ssid(wpa_s
, bssid
);
4727 s
= wpas_p2p_get_persistent_go(wpa_s
);
4729 os_memcpy(bssid
, s
->bssid
, ETH_ALEN
);
4732 *group_iface
= wpas_p2p_create_iface(wpa_s
);
4736 os_memcpy(intended_addr
, bssid
, ETH_ALEN
);
4737 os_memcpy(ssid
, s
->ssid
, s
->ssid_len
);
4738 *ssid_len
= s
->ssid_len
;
4744 static int wpas_remove_stale_groups(void *ctx
, const u8
*peer
, const u8
*go
,
4745 const u8
*ssid
, size_t ssid_len
)
4747 struct wpa_supplicant
*wpa_s
= ctx
;
4749 int save_config
= 0;
4752 /* Start with our first choice of Persistent Groups */
4753 while ((s
= wpas_p2p_get_persistent(wpa_s
, peer
, NULL
, 0))) {
4754 if (go
&& ssid
&& ssid_len
&&
4755 s
->ssid_len
== ssid_len
&&
4756 os_memcmp(go
, s
->bssid
, ETH_ALEN
) == 0 &&
4757 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
4760 /* Remove stale persistent group */
4761 if (s
->mode
!= WPAS_MODE_P2P_GO
|| s
->num_p2p_clients
<= 1) {
4762 wpa_config_remove_network(wpa_s
->conf
, s
->id
);
4767 for (i
= 0; i
< s
->num_p2p_clients
; i
++) {
4768 if (os_memcmp(s
->p2p_client_list
+ i
* 2 * ETH_ALEN
,
4769 peer
, ETH_ALEN
) != 0)
4772 os_memmove(s
->p2p_client_list
+ i
* 2 * ETH_ALEN
,
4773 s
->p2p_client_list
+ (i
+ 1) * 2 * ETH_ALEN
,
4774 (s
->num_p2p_clients
- i
- 1) * 2 * ETH_ALEN
);
4777 s
->num_p2p_clients
--;
4782 p2p_config_write(wpa_s
);
4784 /* Return TRUE if valid SSID remains */
4789 static void wpas_p2ps_prov_complete(void *ctx
, u8 status
, const u8
*dev
,
4790 const u8
*adv_mac
, const u8
*ses_mac
,
4791 const u8
*grp_mac
, u32 adv_id
, u32 ses_id
,
4792 u8 conncap
, int passwd_id
,
4793 const u8
*persist_ssid
,
4794 size_t persist_ssid_size
, int response_done
,
4795 int prov_start
, const char *session_info
)
4797 struct wpa_supplicant
*wpa_s
= ctx
;
4799 struct wpa_ssid
*persistent_go
, *stale
, *s
;
4800 int save_config
= 0;
4801 struct wpa_supplicant
*go_wpa_s
;
4806 os_memset(mac
, 0, ETH_ALEN
);
4815 if (session_info
== NULL
) {
4816 wpa_msg_global(wpa_s
, MSG_INFO
,
4817 P2P_EVENT_P2PS_PROVISION_START MACSTR
4818 " adv_id=%x conncap=%x"
4820 " session=%x mac=" MACSTR
4821 " dev_passwd_id=%d",
4822 MAC2STR(dev
), adv_id
, conncap
,
4824 ses_id
, MAC2STR(ses_mac
),
4827 wpa_msg_global(wpa_s
, MSG_INFO
,
4828 P2P_EVENT_P2PS_PROVISION_START MACSTR
4829 " adv_id=%x conncap=%x"
4831 " session=%x mac=" MACSTR
4832 " dev_passwd_id=%d info='%s'",
4833 MAC2STR(dev
), adv_id
, conncap
,
4835 ses_id
, MAC2STR(ses_mac
),
4836 passwd_id
, session_info
);
4841 go_wpa_s
= wpas_p2p_get_go_group(wpa_s
);
4842 persistent_go
= wpas_p2p_get_persistent_go(wpa_s
);
4844 if (status
&& status
!= P2P_SC_SUCCESS_DEFERRED
) {
4845 if (go_wpa_s
&& !p2p_group_go_member_count(wpa_s
))
4846 wpas_p2p_group_remove(wpa_s
, go_wpa_s
->ifname
);
4848 if (persistent_go
&& !persistent_go
->num_p2p_clients
) {
4849 /* remove empty persistent GO */
4850 wpa_config_remove_network(wpa_s
->conf
,
4854 wpa_msg_global(wpa_s
, MSG_INFO
,
4855 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4857 " adv_id=%x adv_mac=" MACSTR
4858 " session=%x mac=" MACSTR
,
4859 MAC2STR(dev
), status
,
4860 adv_id
, MAC2STR(adv_mac
),
4861 ses_id
, MAC2STR(ses_mac
));
4865 /* Clean up stale persistent groups with this device */
4866 s
= wpas_p2p_get_persistent(wpa_s
, dev
, persist_ssid
,
4869 stale
= wpas_p2p_get_persistent(wpa_s
, dev
, NULL
, 0);
4873 if (s
&& s
->ssid_len
== stale
->ssid_len
&&
4874 os_memcmp(stale
->bssid
, s
->bssid
, ETH_ALEN
) == 0 &&
4875 os_memcmp(stale
->ssid
, s
->ssid
, s
->ssid_len
) == 0)
4878 /* Remove stale persistent group */
4879 if (stale
->mode
!= WPAS_MODE_P2P_GO
||
4880 stale
->num_p2p_clients
<= 1) {
4881 wpa_config_remove_network(wpa_s
->conf
, stale
->id
);
4885 for (i
= 0; i
< stale
->num_p2p_clients
; i
++) {
4886 if (os_memcmp(stale
->p2p_client_list
+
4888 dev
, ETH_ALEN
) == 0) {
4889 os_memmove(stale
->p2p_client_list
+
4891 stale
->p2p_client_list
+
4893 (stale
->num_p2p_clients
-
4898 stale
->num_p2p_clients
--;
4904 p2p_config_write(wpa_s
);
4907 if (go_wpa_s
&& !p2p_group_go_member_count(wpa_s
))
4908 wpas_p2p_group_remove(wpa_s
, go_wpa_s
->ifname
);
4910 if (persistent_go
&& s
!= persistent_go
&&
4911 !persistent_go
->num_p2p_clients
) {
4912 /* remove empty persistent GO */
4913 wpa_config_remove_network(wpa_s
->conf
,
4918 wpa_msg_global(wpa_s
, MSG_INFO
,
4919 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4921 " adv_id=%x adv_mac=" MACSTR
4922 " session=%x mac=" MACSTR
4924 MAC2STR(dev
), status
,
4925 adv_id
, MAC2STR(adv_mac
),
4926 ses_id
, MAC2STR(ses_mac
), s
->id
);
4930 if (conncap
== P2PS_SETUP_GROUP_OWNER
) {
4931 const char *go_ifname
= NULL
;
4933 wpa_s
->global
->pending_p2ps_group
= 1;
4935 if (wpa_s
->conf
->p2p_no_group_iface
)
4936 go_ifname
= wpa_s
->ifname
;
4937 else if (wpa_s
->pending_interface_name
[0])
4938 go_ifname
= wpa_s
->pending_interface_name
;
4941 wpas_p2ps_prov_complete(
4942 wpa_s
, P2P_SC_FAIL_UNKNOWN_GROUP
,
4943 dev
, adv_mac
, ses_mac
,
4944 NULL
, adv_id
, ses_id
, 0, 0,
4945 NULL
, 0, 0, 0, NULL
);
4949 /* If PD Resp complete, start up the GO */
4950 if (response_done
&& persistent_go
) {
4951 wpas_p2p_group_add_persistent(
4952 wpa_s
, persistent_go
,
4953 0, 0, 0, 0, 0, NULL
,
4954 persistent_go
->mode
==
4956 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
:
4958 } else if (response_done
) {
4959 wpas_p2p_group_add(wpa_s
, 1, 0, 0, 0);
4962 if (passwd_id
== DEV_PW_P2PS_DEFAULT
) {
4963 os_memcpy(wpa_s
->p2ps_join_addr
, dev
, ETH_ALEN
);
4964 wpa_s
->p2ps_join_addr_valid
= 1;
4965 wpa_dbg(wpa_s
, MSG_DEBUG
,
4966 "P2PS: Saving PIN for " MACSTR
,
4969 } else if (passwd_id
== DEV_PW_P2PS_DEFAULT
) {
4970 go_ifname
= go_wpa_s
->ifname
;
4972 wpa_dbg(go_wpa_s
, MSG_DEBUG
,
4973 "P2P: Setting PIN-1 For " MACSTR
, MAC2STR(dev
));
4974 wpa_supplicant_ap_wps_pin(go_wpa_s
, dev
, "12345670",
4977 os_memcpy(wpa_s
->p2ps_join_addr
, dev
, ETH_ALEN
);
4978 wpa_s
->p2ps_join_addr_valid
= 1;
4979 wpa_dbg(wpa_s
, MSG_DEBUG
,
4980 "P2PS: Saving PIN for " MACSTR
, MAC2STR(dev
));
4983 wpa_msg_global(wpa_s
, MSG_INFO
,
4984 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4985 " status=%d conncap=%x"
4986 " adv_id=%x adv_mac=" MACSTR
4987 " session=%x mac=" MACSTR
4988 " dev_passwd_id=%d go=%s",
4989 MAC2STR(dev
), status
, conncap
,
4990 adv_id
, MAC2STR(adv_mac
),
4991 ses_id
, MAC2STR(ses_mac
),
4992 passwd_id
, go_ifname
);
4996 if (go_wpa_s
&& !p2p_group_go_member_count(wpa_s
))
4997 wpas_p2p_group_remove(wpa_s
, go_wpa_s
->ifname
);
4999 if (persistent_go
&& !persistent_go
->num_p2p_clients
) {
5000 /* remove empty persistent GO */
5001 wpa_config_remove_network(wpa_s
->conf
, persistent_go
->id
);
5004 if (conncap
== P2PS_SETUP_CLIENT
) {
5005 wpa_msg_global(wpa_s
, MSG_INFO
,
5006 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5007 " status=%d conncap=%x"
5008 " adv_id=%x adv_mac=" MACSTR
5009 " session=%x mac=" MACSTR
5010 " dev_passwd_id=%d join=" MACSTR
,
5011 MAC2STR(dev
), status
, conncap
,
5012 adv_id
, MAC2STR(adv_mac
),
5013 ses_id
, MAC2STR(ses_mac
),
5014 passwd_id
, MAC2STR(grp_mac
));
5016 wpa_msg_global(wpa_s
, MSG_INFO
,
5017 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5018 " status=%d conncap=%x"
5019 " adv_id=%x adv_mac=" MACSTR
5020 " session=%x mac=" MACSTR
5021 " dev_passwd_id=%d",
5022 MAC2STR(dev
), status
, conncap
,
5023 adv_id
, MAC2STR(adv_mac
),
5024 ses_id
, MAC2STR(ses_mac
),
5030 static int _wpas_p2p_in_progress(void *ctx
)
5032 struct wpa_supplicant
*wpa_s
= ctx
;
5033 return wpas_p2p_in_progress(wpa_s
);
5037 static int wpas_prov_disc_resp_cb(void *ctx
)
5039 struct wpa_supplicant
*wpa_s
= ctx
;
5040 struct wpa_ssid
*persistent_go
;
5042 if (!wpa_s
->global
->pending_p2ps_group
)
5045 wpa_s
->global
->pending_p2ps_group
= 0;
5047 if (wpas_p2p_get_go_group(wpa_s
))
5049 persistent_go
= wpas_p2p_get_persistent_go(wpa_s
);
5051 if (persistent_go
) {
5052 wpas_p2p_group_add_persistent(
5053 wpa_s
, persistent_go
, 0, 0, 0, 0, 0, NULL
,
5054 persistent_go
->mode
== WPAS_MODE_P2P_GO
?
5055 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
: 0);
5057 wpas_p2p_group_add(wpa_s
, 1, 0, 0, 0);
5065 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
5066 * @global: Pointer to global data from wpa_supplicant_init()
5067 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5068 * Returns: 0 on success, -1 on failure
5070 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
5072 struct p2p_config p2p
;
5075 if (wpa_s
->conf
->p2p_disabled
)
5078 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
5084 os_memset(&p2p
, 0, sizeof(p2p
));
5086 p2p
.debug_print
= wpas_p2p_debug_print
;
5087 p2p
.p2p_scan
= wpas_p2p_scan
;
5088 p2p
.send_action
= wpas_send_action
;
5089 p2p
.send_action_done
= wpas_send_action_done
;
5090 p2p
.go_neg_completed
= wpas_go_neg_completed
;
5091 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
5092 p2p
.dev_found
= wpas_dev_found
;
5093 p2p
.dev_lost
= wpas_dev_lost
;
5094 p2p
.find_stopped
= wpas_find_stopped
;
5095 p2p
.start_listen
= wpas_start_listen
;
5096 p2p
.stop_listen
= wpas_stop_listen
;
5097 p2p
.send_probe_resp
= wpas_send_probe_resp
;
5098 p2p
.sd_request
= wpas_sd_request
;
5099 p2p
.sd_response
= wpas_sd_response
;
5100 p2p
.prov_disc_req
= wpas_prov_disc_req
;
5101 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
5102 p2p
.prov_disc_fail
= wpas_prov_disc_fail
;
5103 p2p
.invitation_process
= wpas_invitation_process
;
5104 p2p
.invitation_received
= wpas_invitation_received
;
5105 p2p
.invitation_result
= wpas_invitation_result
;
5106 p2p
.get_noa
= wpas_get_noa
;
5107 p2p
.go_connected
= wpas_go_connected
;
5108 p2p
.presence_resp
= wpas_presence_resp
;
5109 p2p
.is_concurrent_session_active
= wpas_is_concurrent_session_active
;
5110 p2p
.is_p2p_in_progress
= _wpas_p2p_in_progress
;
5111 p2p
.get_persistent_group
= wpas_get_persistent_group
;
5112 p2p
.get_go_info
= wpas_get_go_info
;
5113 p2p
.remove_stale_groups
= wpas_remove_stale_groups
;
5114 p2p
.p2ps_prov_complete
= wpas_p2ps_prov_complete
;
5115 p2p
.prov_disc_resp_cb
= wpas_prov_disc_resp_cb
;
5116 p2p
.p2ps_group_capability
= p2ps_group_capability
;
5118 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
5119 os_memcpy(p2p
.dev_addr
, wpa_s
->global
->p2p_dev_addr
, ETH_ALEN
);
5120 p2p
.dev_name
= wpa_s
->conf
->device_name
;
5121 p2p
.manufacturer
= wpa_s
->conf
->manufacturer
;
5122 p2p
.model_name
= wpa_s
->conf
->model_name
;
5123 p2p
.model_number
= wpa_s
->conf
->model_number
;
5124 p2p
.serial_number
= wpa_s
->conf
->serial_number
;
5126 os_memcpy(p2p
.uuid
, wpa_s
->wps
->uuid
, 16);
5127 p2p
.config_methods
= wpa_s
->wps
->config_methods
;
5130 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
, &p2p
.cli_channels
)) {
5131 wpa_printf(MSG_ERROR
,
5132 "P2P: Failed to configure supported channel list");
5136 if (wpa_s
->conf
->p2p_listen_reg_class
&&
5137 wpa_s
->conf
->p2p_listen_channel
) {
5138 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
5139 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
5140 p2p
.channel_forced
= 1;
5143 * Pick one of the social channels randomly as the listen
5146 if (p2p_config_get_random_social(&p2p
, &p2p
.reg_class
,
5147 &p2p
.channel
) != 0) {
5148 wpa_printf(MSG_ERROR
,
5149 "P2P: Failed to select random social channel as listen channel");
5152 p2p
.channel_forced
= 0;
5154 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d:%d",
5155 p2p
.reg_class
, p2p
.channel
);
5157 if (wpa_s
->conf
->p2p_oper_reg_class
&&
5158 wpa_s
->conf
->p2p_oper_channel
) {
5159 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
5160 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
5161 p2p
.cfg_op_channel
= 1;
5162 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
5163 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
5167 * Use random operation channel from 2.4 GHz band social
5168 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5169 * other preference is indicated.
5171 if (p2p_config_get_random_social(&p2p
, &p2p
.op_reg_class
,
5172 &p2p
.op_channel
) != 0) {
5173 wpa_printf(MSG_ERROR
,
5174 "P2P: Failed to select random social channel as operation channel");
5177 p2p
.cfg_op_channel
= 0;
5178 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
5179 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
5182 if (wpa_s
->conf
->p2p_pref_chan
&& wpa_s
->conf
->num_p2p_pref_chan
) {
5183 p2p
.pref_chan
= wpa_s
->conf
->p2p_pref_chan
;
5184 p2p
.num_pref_chan
= wpa_s
->conf
->num_p2p_pref_chan
;
5187 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
5188 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
5189 p2p
.country
[2] = 0x04;
5191 os_memcpy(p2p
.country
, "XX\x04", 3);
5193 os_memcpy(p2p
.pri_dev_type
, wpa_s
->conf
->device_type
,
5196 p2p
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
5197 os_memcpy(p2p
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
5198 p2p
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
5200 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
5201 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
5203 p2p
.max_peers
= 100;
5205 if (wpa_s
->conf
->p2p_ssid_postfix
) {
5206 p2p
.ssid_postfix_len
=
5207 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
5208 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
5209 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
5210 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
5211 p2p
.ssid_postfix_len
);
5214 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
5216 p2p
.max_listen
= wpa_s
->max_remain_on_chan
;
5218 if (wpa_s
->conf
->p2p_passphrase_len
>= 8 &&
5219 wpa_s
->conf
->p2p_passphrase_len
<= 63)
5220 p2p
.passphrase_len
= wpa_s
->conf
->p2p_passphrase_len
;
5222 p2p
.passphrase_len
= 8;
5224 global
->p2p
= p2p_init(&p2p
);
5225 if (global
->p2p
== NULL
)
5227 global
->p2p_init_wpa_s
= wpa_s
;
5229 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
5230 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
5232 p2p_add_wps_vendor_extension(
5233 global
->p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
5236 p2p_set_no_go_freq(global
->p2p
, &wpa_s
->conf
->p2p_no_go_freq
);
5243 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5244 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5246 * This function deinitialize per-interface P2P data.
5248 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
5250 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
5251 wpa_drv_probe_req_report(wpa_s
, 0);
5253 if (wpa_s
->go_params
) {
5254 /* Clear any stored provisioning info */
5255 p2p_clear_provisioning_info(
5257 wpa_s
->go_params
->peer_device_addr
);
5260 os_free(wpa_s
->go_params
);
5261 wpa_s
->go_params
= NULL
;
5262 eloop_cancel_timeout(wpas_p2p_psk_failure_removal
, wpa_s
, NULL
);
5263 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
5264 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
5265 wpa_s
->p2p_long_listen
= 0;
5266 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
5267 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
5268 wpas_p2p_remove_pending_group_interface(wpa_s
);
5269 eloop_cancel_timeout(wpas_p2p_group_freq_conflict
, wpa_s
, NULL
);
5270 wpas_p2p_listen_work_done(wpa_s
);
5271 if (wpa_s
->p2p_send_action_work
) {
5272 os_free(wpa_s
->p2p_send_action_work
->ctx
);
5273 radio_work_done(wpa_s
->p2p_send_action_work
);
5274 wpa_s
->p2p_send_action_work
= NULL
;
5276 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout
, wpa_s
, NULL
);
5278 wpabuf_free(wpa_s
->p2p_oob_dev_pw
);
5279 wpa_s
->p2p_oob_dev_pw
= NULL
;
5281 os_free(wpa_s
->p2p_group_common_freqs
);
5282 wpa_s
->p2p_group_common_freqs
= NULL
;
5283 wpa_s
->p2p_group_common_freqs_num
= 0;
5285 /* TODO: remove group interface from the driver if this wpa_s instance
5286 * is on top of a P2P group interface */
5291 * wpas_p2p_deinit_global - Deinitialize global P2P module
5292 * @global: Pointer to global data from wpa_supplicant_init()
5294 * This function deinitializes the global (per device) P2P module.
5296 static void wpas_p2p_deinit_global(struct wpa_global
*global
)
5298 struct wpa_supplicant
*wpa_s
, *tmp
;
5300 wpa_s
= global
->ifaces
;
5302 wpas_p2p_service_flush(global
->p2p_init_wpa_s
);
5304 /* Remove remaining P2P group interfaces */
5305 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
5306 wpa_s
= wpa_s
->next
;
5308 tmp
= global
->ifaces
;
5311 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
5316 /* Disconnect from the P2P group and deinit the interface */
5317 wpas_p2p_disconnect(tmp
);
5321 * Deinit GO data on any possibly remaining interface (if main
5322 * interface is used as GO).
5324 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
5325 if (wpa_s
->ap_iface
)
5326 wpas_p2p_group_deinit(wpa_s
);
5329 p2p_deinit(global
->p2p
);
5331 global
->p2p_init_wpa_s
= NULL
;
5335 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
5337 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE
) &&
5338 wpa_s
->conf
->p2p_no_group_iface
)
5339 return 0; /* separate interface disabled per configuration */
5340 if (wpa_s
->drv_flags
&
5341 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
5342 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
5343 return 1; /* P2P group requires a new interface in every case
5345 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
5346 return 0; /* driver does not support concurrent operations */
5347 if (wpa_s
->global
->ifaces
->next
)
5348 return 1; /* more that one interface already in use */
5349 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
5350 return 1; /* this interface is already in use */
5355 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
5356 const u8
*peer_addr
,
5357 enum p2p_wps_method wps_method
,
5358 int go_intent
, const u8
*own_interface_addr
,
5359 unsigned int force_freq
, int persistent_group
,
5360 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
5362 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
5363 persistent_group
= 2;
5366 * Increase GO config timeout if HT40 is used since it takes some time
5367 * to scan channels for coex purposes before the BSS can be started.
5369 p2p_set_config_timeout(wpa_s
->global
->p2p
,
5370 wpa_s
->p2p_go_ht40
? 255 : 100, 20);
5372 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
5373 go_intent
, own_interface_addr
, force_freq
,
5374 persistent_group
, ssid
? ssid
->ssid
: NULL
,
5375 ssid
? ssid
->ssid_len
: 0,
5376 wpa_s
->p2p_pd_before_go_neg
, pref_freq
,
5377 wps_method
== WPS_NFC
? wpa_s
->p2p_oob_dev_pw_id
:
5382 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
5383 const u8
*peer_addr
,
5384 enum p2p_wps_method wps_method
,
5385 int go_intent
, const u8
*own_interface_addr
,
5386 unsigned int force_freq
, int persistent_group
,
5387 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
5389 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
5390 persistent_group
= 2;
5392 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
5393 go_intent
, own_interface_addr
, force_freq
,
5394 persistent_group
, ssid
? ssid
->ssid
: NULL
,
5395 ssid
? ssid
->ssid_len
: 0, pref_freq
,
5396 wps_method
== WPS_NFC
? wpa_s
->p2p_oob_dev_pw_id
:
5401 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
5403 wpa_s
->p2p_join_scan_count
++;
5404 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
5405 wpa_s
->p2p_join_scan_count
);
5406 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
5407 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
5408 " for join operationg - stop join attempt",
5409 MAC2STR(wpa_s
->pending_join_iface_addr
));
5410 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
5411 if (wpa_s
->p2p_auto_pd
) {
5412 wpa_s
->p2p_auto_pd
= 0;
5413 wpa_msg_global(wpa_s
, MSG_INFO
,
5414 P2P_EVENT_PROV_DISC_FAILURE
5415 " p2p_dev_addr=" MACSTR
" status=N/A",
5416 MAC2STR(wpa_s
->pending_join_dev_addr
));
5419 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
5420 P2P_EVENT_GROUP_FORMATION_FAILURE
);
5425 static int wpas_check_freq_conflict(struct wpa_supplicant
*wpa_s
, int freq
)
5428 unsigned int num
, i
;
5429 struct wpa_used_freq_data
*freqs
;
5431 if (wpas_p2p_num_unused_channels(wpa_s
) > 0) {
5432 /* Multiple channels are supported and not all are in use */
5436 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
,
5437 sizeof(struct wpa_used_freq_data
));
5441 num
= wpas_p2p_valid_oper_freqs(wpa_s
, freqs
,
5442 wpa_s
->num_multichan_concurrent
);
5444 for (i
= 0; i
< num
; i
++) {
5445 if (freqs
[i
].freq
== freq
) {
5446 wpa_printf(MSG_DEBUG
, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5453 wpa_printf(MSG_DEBUG
, "P2P: No valid operating frequencies");
5462 static int wpas_p2p_peer_go(struct wpa_supplicant
*wpa_s
,
5463 const u8
*peer_dev_addr
)
5465 struct wpa_bss
*bss
;
5468 bss
= wpa_bss_get_p2p_dev_addr(wpa_s
, peer_dev_addr
);
5471 if (bss
->last_update_idx
< wpa_s
->bss_update_idx
) {
5472 wpa_printf(MSG_DEBUG
, "P2P: Peer BSS entry not updated in the "
5477 updated
= os_reltime_before(&wpa_s
->p2p_auto_started
,
5479 wpa_printf(MSG_DEBUG
, "P2P: Current BSS entry for peer updated at "
5480 "%ld.%06ld (%supdated in last scan)",
5481 bss
->last_update
.sec
, bss
->last_update
.usec
,
5482 updated
? "": "not ");
5488 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
5489 struct wpa_scan_results
*scan_res
)
5491 struct wpa_bss
*bss
= NULL
;
5493 u8 iface_addr
[ETH_ALEN
];
5495 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
5497 if (wpa_s
->global
->p2p_disabled
)
5500 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for %sjoin",
5501 scan_res
? (int) scan_res
->num
: -1,
5502 wpa_s
->p2p_auto_join
? "auto_" : "");
5505 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
5507 if (wpa_s
->p2p_auto_pd
) {
5508 int join
= wpas_p2p_peer_go(wpa_s
,
5509 wpa_s
->pending_join_dev_addr
);
5511 wpa_s
->auto_pd_scan_retry
< P2P_AUTO_PD_SCAN_ATTEMPTS
) {
5512 wpa_s
->auto_pd_scan_retry
++;
5513 bss
= wpa_bss_get_bssid_latest(
5514 wpa_s
, wpa_s
->pending_join_dev_addr
);
5517 wpa_printf(MSG_DEBUG
, "P2P: Scan retry %d for "
5518 "the peer " MACSTR
" at %d MHz",
5519 wpa_s
->auto_pd_scan_retry
,
5521 pending_join_dev_addr
),
5523 wpas_p2p_join_scan_req(wpa_s
, freq
, NULL
, 0);
5531 wpa_s
->p2p_auto_pd
= 0;
5532 wpa_s
->pending_pd_use
= join
? AUTO_PD_JOIN
: AUTO_PD_GO_NEG
;
5533 wpa_printf(MSG_DEBUG
, "P2P: Auto PD with " MACSTR
" join=%d",
5534 MAC2STR(wpa_s
->pending_join_dev_addr
), join
);
5535 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
5536 wpa_s
->pending_join_dev_addr
, NULL
,
5537 wpa_s
->pending_pd_config_methods
, join
,
5538 0, wpa_s
->user_initiated_pd
) < 0) {
5539 wpa_s
->p2p_auto_pd
= 0;
5540 wpa_msg_global(wpa_s
, MSG_INFO
,
5541 P2P_EVENT_PROV_DISC_FAILURE
5542 " p2p_dev_addr=" MACSTR
" status=N/A",
5543 MAC2STR(wpa_s
->pending_join_dev_addr
));
5548 if (wpa_s
->p2p_auto_join
) {
5549 int join
= wpas_p2p_peer_go(wpa_s
,
5550 wpa_s
->pending_join_dev_addr
);
5552 wpa_printf(MSG_DEBUG
, "P2P: Peer was not found to be "
5553 "running a GO -> use GO Negotiation");
5554 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
5555 P2P_EVENT_FALLBACK_TO_GO_NEG
5556 "reason=peer-not-running-GO");
5557 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
,
5558 wpa_s
->p2p_pin
, wpa_s
->p2p_wps_method
,
5559 wpa_s
->p2p_persistent_group
, 0, 0, 0,
5560 wpa_s
->p2p_go_intent
,
5561 wpa_s
->p2p_connect_freq
,
5562 wpa_s
->p2p_persistent_id
,
5563 wpa_s
->p2p_pd_before_go_neg
,
5569 wpa_printf(MSG_DEBUG
, "P2P: Peer was found running GO%s -> "
5570 "try to join the group", join
? "" :
5573 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
5574 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED
);
5575 wpa_s
->p2p_fallback_to_go_neg
= 1;
5579 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
5580 wpa_s
->pending_join_iface_addr
);
5582 p2p_get_interface_addr(wpa_s
->global
->p2p
,
5583 wpa_s
->pending_join_dev_addr
,
5585 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0
5586 && !wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
)) {
5587 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
5588 "address for join from " MACSTR
" to " MACSTR
5589 " based on newly discovered P2P peer entry",
5590 MAC2STR(wpa_s
->pending_join_iface_addr
),
5591 MAC2STR(iface_addr
));
5592 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
5595 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
5596 wpa_s
->pending_join_iface_addr
);
5599 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
5600 "from P2P peer table: %d MHz", freq
);
5602 if (wpa_s
->p2p_join_ssid_len
) {
5603 wpa_printf(MSG_DEBUG
, "P2P: Trying to find target GO BSS entry based on BSSID "
5604 MACSTR
" and SSID %s",
5605 MAC2STR(wpa_s
->pending_join_iface_addr
),
5606 wpa_ssid_txt(wpa_s
->p2p_join_ssid
,
5607 wpa_s
->p2p_join_ssid_len
));
5608 bss
= wpa_bss_get(wpa_s
, wpa_s
->pending_join_iface_addr
,
5609 wpa_s
->p2p_join_ssid
,
5610 wpa_s
->p2p_join_ssid_len
);
5613 wpa_printf(MSG_DEBUG
, "P2P: Trying to find target GO BSS entry based on BSSID "
5614 MACSTR
, MAC2STR(wpa_s
->pending_join_iface_addr
));
5615 bss
= wpa_bss_get_bssid_latest(wpa_s
,
5616 wpa_s
->pending_join_iface_addr
);
5620 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
5621 "from BSS table: %d MHz (SSID %s)", freq
,
5622 wpa_ssid_txt(bss
->ssid
, bss
->ssid_len
));
5627 if (wpas_check_freq_conflict(wpa_s
, freq
) > 0) {
5628 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
5629 P2P_EVENT_GROUP_FORMATION_FAILURE
5630 "reason=FREQ_CONFLICT");
5634 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
5635 "prior to joining an existing group (GO " MACSTR
5637 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
5638 wpa_s
->pending_pd_before_join
= 1;
5640 switch (wpa_s
->pending_join_wps_method
) {
5641 case WPS_PIN_DISPLAY
:
5642 method
= WPS_CONFIG_KEYPAD
;
5644 case WPS_PIN_KEYPAD
:
5645 method
= WPS_CONFIG_DISPLAY
;
5648 method
= WPS_CONFIG_PUSHBUTTON
;
5655 if ((p2p_get_provisioning_info(wpa_s
->global
->p2p
,
5656 wpa_s
->pending_join_dev_addr
) ==
5659 * We have already performed provision discovery for
5660 * joining the group. Proceed directly to join
5661 * operation without duplicated provision discovery. */
5662 wpa_printf(MSG_DEBUG
, "P2P: Provision discovery "
5663 "with " MACSTR
" already done - proceed to "
5665 MAC2STR(wpa_s
->pending_join_dev_addr
));
5666 wpa_s
->pending_pd_before_join
= 0;
5670 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
5671 wpa_s
->pending_join_dev_addr
,
5673 freq
, wpa_s
->user_initiated_pd
) < 0) {
5674 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
5675 "Discovery Request before joining an "
5677 wpa_s
->pending_pd_before_join
= 0;
5683 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
5684 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
5685 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
5686 wpas_p2p_check_join_scan_limit(wpa_s
);
5690 /* Start join operation immediately */
5691 wpas_p2p_join_start(wpa_s
, 0, NULL
, 0);
5695 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
,
5696 const u8
*ssid
, size_t ssid_len
)
5699 struct wpa_driver_scan_params params
;
5700 struct wpabuf
*wps_ie
, *ies
;
5702 int freqs
[2] = { 0, 0 };
5704 os_memset(¶ms
, 0, sizeof(params
));
5706 /* P2P Wildcard SSID */
5707 params
.num_ssids
= 1;
5708 if (ssid
&& ssid_len
) {
5709 params
.ssids
[0].ssid
= ssid
;
5710 params
.ssids
[0].ssid_len
= ssid_len
;
5711 os_memcpy(wpa_s
->p2p_join_ssid
, ssid
, ssid_len
);
5712 wpa_s
->p2p_join_ssid_len
= ssid_len
;
5714 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
5715 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
5716 wpa_s
->p2p_join_ssid_len
= 0;
5719 wpa_s
->wps
->dev
.p2p
= 1;
5720 wps_ie
= wps_build_probe_req_ie(DEV_PW_DEFAULT
, &wpa_s
->wps
->dev
,
5721 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
, 0,
5723 if (wps_ie
== NULL
) {
5724 wpas_p2p_scan_res_join(wpa_s
, NULL
);
5728 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
5729 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
5731 wpabuf_free(wps_ie
);
5732 wpas_p2p_scan_res_join(wpa_s
, NULL
);
5735 wpabuf_put_buf(ies
, wps_ie
);
5736 wpabuf_free(wps_ie
);
5738 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
5740 params
.p2p_probe
= 1;
5741 params
.extra_ies
= wpabuf_head(ies
);
5742 params
.extra_ies_len
= wpabuf_len(ies
);
5747 * If freq is not provided, check the operating freq of the GO
5748 * and use a single channel scan on if possible.
5750 oper_freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
5751 wpa_s
->pending_join_iface_addr
);
5757 params
.freqs
= freqs
;
5761 * Run a scan to update BSS table and start Provision Discovery once
5762 * the new scan results become available.
5764 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
5766 os_get_reltime(&wpa_s
->scan_trigger_time
);
5767 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
5768 wpa_s
->own_scan_requested
= 1;
5774 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
5776 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
5777 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
5778 wpas_p2p_check_join_scan_limit(wpa_s
);
5783 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
5785 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
5786 wpas_p2p_join_scan_req(wpa_s
, 0, NULL
, 0);
5790 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
5791 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
5792 int auto_join
, int op_freq
,
5793 const u8
*ssid
, size_t ssid_len
)
5795 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
5796 MACSTR
" dev " MACSTR
" op_freq=%d)%s",
5797 MAC2STR(iface_addr
), MAC2STR(dev_addr
), op_freq
,
5798 auto_join
? " (auto_join)" : "");
5799 if (ssid
&& ssid_len
) {
5800 wpa_printf(MSG_DEBUG
, "P2P: Group SSID specified: %s",
5801 wpa_ssid_txt(ssid
, ssid_len
));
5804 wpa_s
->p2p_auto_pd
= 0;
5805 wpa_s
->p2p_auto_join
= !!auto_join
;
5806 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
5807 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
5808 wpa_s
->pending_join_wps_method
= wps_method
;
5810 /* Make sure we are not running find during connection establishment */
5811 wpas_p2p_stop_find(wpa_s
);
5813 wpa_s
->p2p_join_scan_count
= 0;
5814 wpas_p2p_join_scan_req(wpa_s
, op_freq
, ssid
, ssid_len
);
5819 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
, int freq
,
5820 const u8
*ssid
, size_t ssid_len
)
5822 struct wpa_supplicant
*group
;
5823 struct p2p_go_neg_results res
;
5824 struct wpa_bss
*bss
;
5826 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
5829 if (group
!= wpa_s
) {
5830 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
5831 sizeof(group
->p2p_pin
));
5832 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
5835 * Need to mark the current interface for p2p_group_formation
5836 * when a separate group interface is not used. This is needed
5837 * to allow p2p_cancel stop a pending p2p_connect-join.
5838 * wpas_p2p_init_group_interface() addresses this for the case
5839 * where a separate group interface is used.
5841 wpa_s
->global
->p2p_group_formation
= wpa_s
;
5844 group
->p2p_in_provisioning
= 1;
5845 group
->p2p_fallback_to_go_neg
= wpa_s
->p2p_fallback_to_go_neg
;
5847 os_memset(&res
, 0, sizeof(res
));
5848 os_memcpy(res
.peer_device_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
);
5849 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
5851 res
.wps_method
= wpa_s
->pending_join_wps_method
;
5852 if (freq
&& ssid
&& ssid_len
) {
5854 res
.ssid_len
= ssid_len
;
5855 os_memcpy(res
.ssid
, ssid
, ssid_len
);
5857 bss
= wpa_bss_get_bssid_latest(wpa_s
,
5858 wpa_s
->pending_join_iface_addr
);
5860 res
.freq
= bss
->freq
;
5861 res
.ssid_len
= bss
->ssid_len
;
5862 os_memcpy(res
.ssid
, bss
->ssid
, bss
->ssid_len
);
5863 wpa_printf(MSG_DEBUG
, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5865 wpa_ssid_txt(bss
->ssid
, bss
->ssid_len
));
5869 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
5870 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel prior to "
5872 wpa_drv_cancel_remain_on_channel(wpa_s
);
5873 wpa_s
->off_channel_freq
= 0;
5874 wpa_s
->roc_waiting_drv_freq
= 0;
5876 wpas_start_wps_enrollee(group
, &res
);
5879 * Allow a longer timeout for join-a-running-group than normal 15
5880 * second group formation timeout since the GO may not have authorized
5881 * our connection yet.
5883 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
5884 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
5891 static int wpas_p2p_setup_freqs(struct wpa_supplicant
*wpa_s
, int freq
,
5892 int *force_freq
, int *pref_freq
, int go
)
5894 struct wpa_used_freq_data
*freqs
;
5895 int res
, best_freq
, num_unused
;
5896 unsigned int freq_in_use
= 0, num
, i
;
5898 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
,
5899 sizeof(struct wpa_used_freq_data
));
5903 num
= wpas_p2p_valid_oper_freqs(wpa_s
, freqs
,
5904 wpa_s
->num_multichan_concurrent
);
5907 * It is possible that the total number of used frequencies is bigger
5908 * than the number of frequencies used for P2P, so get the system wide
5909 * number of unused frequencies.
5911 num_unused
= wpas_p2p_num_unused_channels(wpa_s
);
5913 wpa_printf(MSG_DEBUG
,
5914 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5915 freq
, wpa_s
->num_multichan_concurrent
, num
, num_unused
);
5920 ret
= p2p_supported_freq(wpa_s
->global
->p2p
, freq
);
5922 ret
= p2p_supported_freq_cli(wpa_s
->global
->p2p
, freq
);
5924 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
5925 "(%u MHz) is not supported for P2P uses",
5931 for (i
= 0; i
< num
; i
++) {
5932 if (freqs
[i
].freq
== freq
)
5936 if (num_unused
<= 0 && !freq_in_use
) {
5937 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5942 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
5943 "requested channel (%u MHz)", freq
);
5948 best_freq
= wpas_p2p_pick_best_used_freq(wpa_s
, freqs
, num
);
5950 /* We have a candidate frequency to use */
5951 if (best_freq
> 0) {
5952 if (*pref_freq
== 0 && num_unused
> 0) {
5953 wpa_printf(MSG_DEBUG
, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5955 *pref_freq
= best_freq
;
5957 wpa_printf(MSG_DEBUG
, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5959 *force_freq
= best_freq
;
5961 } else if (num_unused
> 0) {
5962 wpa_printf(MSG_DEBUG
,
5963 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5966 wpa_printf(MSG_DEBUG
,
5967 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5981 * wpas_p2p_connect - Request P2P Group Formation to be started
5982 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5983 * @peer_addr: Address of the peer P2P Device
5984 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5985 * @persistent_group: Whether to create a persistent group
5986 * @auto_join: Whether to select join vs. GO Negotiation automatically
5987 * @join: Whether to join an existing group (as a client) instead of starting
5988 * Group Owner negotiation; @peer_addr is BSSID in that case
5989 * @auth: Whether to only authorize the connection instead of doing that and
5990 * initiating Group Owner negotiation
5991 * @go_intent: GO Intent or -1 to use default
5992 * @freq: Frequency for the group or 0 for auto-selection
5993 * @persistent_id: Persistent group credentials to use for forcing GO
5994 * parameters or -1 to generate new values (SSID/passphrase)
5995 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5996 * interoperability workaround when initiating group formation
5997 * @ht40: Start GO with 40 MHz channel width
5998 * @vht: Start GO with VHT support
5999 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6000 * failure, -2 on failure due to channel not currently available,
6001 * -3 if forced channel is not supported
6003 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
6004 const char *pin
, enum p2p_wps_method wps_method
,
6005 int persistent_group
, int auto_join
, int join
, int auth
,
6006 int go_intent
, int freq
, int persistent_id
, int pd
,
6009 int force_freq
= 0, pref_freq
= 0;
6011 enum wpa_driver_if_type iftype
;
6013 struct wpa_ssid
*ssid
= NULL
;
6015 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
6018 if (persistent_id
>= 0) {
6019 ssid
= wpa_config_get_network(wpa_s
->conf
, persistent_id
);
6020 if (ssid
== NULL
|| ssid
->disabled
!= 2 ||
6021 ssid
->mode
!= WPAS_MODE_P2P_GO
)
6025 os_free(wpa_s
->global
->add_psk
);
6026 wpa_s
->global
->add_psk
= NULL
;
6028 wpa_s
->global
->p2p_fail_on_wps_complete
= 0;
6029 wpa_s
->global
->pending_p2ps_group
= 0;
6032 go_intent
= wpa_s
->conf
->p2p_go_intent
;
6035 wpa_s
->p2p_long_listen
= 0;
6037 wpa_s
->p2p_wps_method
= wps_method
;
6038 wpa_s
->p2p_persistent_group
= !!persistent_group
;
6039 wpa_s
->p2p_persistent_id
= persistent_id
;
6040 wpa_s
->p2p_go_intent
= go_intent
;
6041 wpa_s
->p2p_connect_freq
= freq
;
6042 wpa_s
->p2p_fallback_to_go_neg
= 0;
6043 wpa_s
->p2p_pd_before_go_neg
= !!pd
;
6044 wpa_s
->p2p_go_ht40
= !!ht40
;
6045 wpa_s
->p2p_go_vht
= !!vht
;
6048 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
6049 else if (wps_method
== WPS_PIN_DISPLAY
) {
6050 ret
= wps_generate_pin();
6051 res
= os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
),
6053 if (os_snprintf_error(sizeof(wpa_s
->p2p_pin
), res
))
6054 wpa_s
->p2p_pin
[sizeof(wpa_s
->p2p_pin
) - 1] = '\0';
6055 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
6058 wpa_s
->p2p_pin
[0] = '\0';
6060 if (join
|| auto_join
) {
6061 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
6063 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
6064 "connect a running group from " MACSTR
,
6065 MAC2STR(peer_addr
));
6066 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
6069 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
6070 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
6072 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
6073 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
6077 os_get_reltime(&wpa_s
->p2p_auto_started
);
6078 wpa_printf(MSG_DEBUG
, "P2P: Auto join started at "
6080 wpa_s
->p2p_auto_started
.sec
,
6081 wpa_s
->p2p_auto_started
.usec
);
6083 wpa_s
->user_initiated_pd
= 1;
6084 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
,
6085 auto_join
, freq
, NULL
, 0) < 0)
6090 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
6094 wpas_p2p_set_own_freq_preference(wpa_s
,
6095 force_freq
? force_freq
: pref_freq
);
6097 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
6099 if (wpa_s
->create_p2p_iface
) {
6100 /* Prepare to add a new interface for the group */
6101 iftype
= WPA_IF_P2P_GROUP
;
6102 if (go_intent
== 15)
6103 iftype
= WPA_IF_P2P_GO
;
6104 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
6105 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
6106 "interface for the group");
6110 if_addr
= wpa_s
->pending_interface_addr
;
6112 if_addr
= wpa_s
->own_addr
;
6115 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
6117 force_freq
, persistent_group
, ssid
,
6123 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
6124 go_intent
, if_addr
, force_freq
,
6125 persistent_group
, ssid
, pref_freq
) < 0) {
6126 if (wpa_s
->create_p2p_iface
)
6127 wpas_p2p_remove_pending_group_interface(wpa_s
);
6135 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6136 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6137 * @freq: Frequency of the channel in MHz
6138 * @duration: Duration of the stay on the channel in milliseconds
6140 * This callback is called when the driver indicates that it has started the
6141 * requested remain-on-channel duration.
6143 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
6144 unsigned int freq
, unsigned int duration
)
6146 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
6148 wpa_printf(MSG_DEBUG
, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
6149 wpa_s
->off_channel_freq
, wpa_s
->pending_listen_freq
,
6150 wpa_s
->roc_waiting_drv_freq
, freq
, duration
);
6151 if (wpa_s
->off_channel_freq
&&
6152 wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
6153 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
6154 wpa_s
->pending_listen_duration
);
6155 wpa_s
->pending_listen_freq
= 0;
6157 wpa_printf(MSG_DEBUG
, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6158 wpa_s
->off_channel_freq
, wpa_s
->pending_listen_freq
,
6164 int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
6166 /* Limit maximum Listen state time based on driver limitation. */
6167 if (timeout
> wpa_s
->max_remain_on_chan
)
6168 timeout
= wpa_s
->max_remain_on_chan
;
6170 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
6175 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6176 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6177 * @freq: Frequency of the channel in MHz
6179 * This callback is called when the driver indicates that a remain-on-channel
6180 * operation has been completed, i.e., the duration on the requested channel
6183 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
6186 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
6187 "(p2p_long_listen=%d ms pending_action_tx=%p)",
6188 wpa_s
->p2p_long_listen
, offchannel_pending_action_tx(wpa_s
));
6189 wpas_p2p_listen_work_done(wpa_s
);
6190 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
6192 if (wpa_s
->p2p_long_listen
> 0)
6193 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
6194 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
6195 return; /* P2P module started a new operation */
6196 if (offchannel_pending_action_tx(wpa_s
))
6198 if (wpa_s
->p2p_long_listen
> 0) {
6199 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
6200 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
6203 * When listen duration is over, stop listen & update p2p_state
6206 p2p_stop_listen(wpa_s
->global
->p2p
);
6212 * wpas_p2p_group_remove - Remove a P2P group
6213 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6214 * @ifname: Network interface name of the group interface or "*" to remove all
6216 * Returns: 0 on success, -1 on failure
6218 * This function is used to remove a P2P group. This can be used to disconnect
6219 * from a group in which the local end is a P2P Client or to end a P2P Group in
6220 * case the local end is the Group Owner. If a virtual network interface was
6221 * created for this group, that interface will be removed. Otherwise, only the
6222 * configured P2P group network will be removed from the interface.
6224 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
6226 struct wpa_global
*global
= wpa_s
->global
;
6227 struct wpa_supplicant
*calling_wpa_s
= wpa_s
;
6229 if (os_strcmp(ifname
, "*") == 0) {
6230 struct wpa_supplicant
*prev
;
6231 wpa_s
= global
->ifaces
;
6234 wpa_s
= wpa_s
->next
;
6235 if (prev
->p2p_group_interface
!=
6236 NOT_P2P_GROUP_INTERFACE
||
6237 (prev
->current_ssid
&&
6238 prev
->current_ssid
->p2p_group
))
6239 wpas_p2p_disconnect_safely(prev
, calling_wpa_s
);
6244 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
6245 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
6249 return wpas_p2p_disconnect_safely(wpa_s
, calling_wpa_s
);
6253 static int wpas_p2p_select_go_freq(struct wpa_supplicant
*wpa_s
, int freq
)
6258 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
6260 if (wpa_s
->best_24_freq
> 0 &&
6261 p2p_supported_freq_go(wpa_s
->global
->p2p
,
6262 wpa_s
->best_24_freq
)) {
6263 freq
= wpa_s
->best_24_freq
;
6264 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
6265 "channel: %d MHz", freq
);
6267 if (os_get_random((u8
*) &r
, sizeof(r
)) < 0)
6269 freq
= 2412 + (r
% 3) * 25;
6270 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
6271 "channel: %d MHz", freq
);
6276 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
6278 if (wpa_s
->best_5_freq
> 0 &&
6279 p2p_supported_freq_go(wpa_s
->global
->p2p
,
6280 wpa_s
->best_5_freq
)) {
6281 freq
= wpa_s
->best_5_freq
;
6282 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
6283 "channel: %d MHz", freq
);
6285 if (os_get_random((u8
*) &r
, sizeof(r
)) < 0)
6287 freq
= 5180 + (r
% 4) * 20;
6288 if (!p2p_supported_freq_go(wpa_s
->global
->p2p
, freq
)) {
6289 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
6290 "5 GHz channel for P2P group");
6293 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
6294 "channel: %d MHz", freq
);
6298 if (freq
> 0 && !p2p_supported_freq_go(wpa_s
->global
->p2p
, freq
)) {
6299 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
6300 "(%u MHz) is not supported for P2P uses",
6309 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant
*wpa_s
,
6310 struct p2p_go_neg_results
*params
,
6311 const struct p2p_channels
*channels
)
6315 /* first try some random selection of the social channels */
6316 if (os_get_random((u8
*) &r
, sizeof(r
)) < 0)
6319 for (i
= 0; i
< 3; i
++) {
6320 params
->freq
= 2412 + ((r
+ i
) % 3) * 25;
6321 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, params
->freq
) &&
6322 freq_included(channels
, params
->freq
) &&
6323 p2p_supported_freq(wpa_s
->global
->p2p
, params
->freq
))
6327 /* try all channels in reg. class 81 */
6328 for (i
= 0; i
< 11; i
++) {
6329 params
->freq
= 2412 + i
* 5;
6330 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, params
->freq
) &&
6331 freq_included(channels
, params
->freq
) &&
6332 p2p_supported_freq(wpa_s
->global
->p2p
, params
->freq
))
6336 /* try social channel class 180 channel 2 */
6337 params
->freq
= 58320 + 1 * 2160;
6338 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, params
->freq
) &&
6339 freq_included(channels
, params
->freq
) &&
6340 p2p_supported_freq(wpa_s
->global
->p2p
, params
->freq
))
6343 /* try all channels in reg. class 180 */
6344 for (i
= 0; i
< 4; i
++) {
6345 params
->freq
= 58320 + i
* 2160;
6346 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, params
->freq
) &&
6347 freq_included(channels
, params
->freq
) &&
6348 p2p_supported_freq(wpa_s
->global
->p2p
, params
->freq
))
6352 wpa_printf(MSG_DEBUG
, "P2P: No 2.4 and 60 GHz channel allowed");
6355 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference known)",
6361 static int wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
6362 struct p2p_go_neg_results
*params
,
6363 int freq
, int ht40
, int vht
,
6364 const struct p2p_channels
*channels
)
6366 struct wpa_used_freq_data
*freqs
;
6367 unsigned int pref_freq
, cand_freq
;
6368 unsigned int num
, i
;
6370 os_memset(params
, 0, sizeof(*params
));
6371 params
->role_go
= 1;
6372 params
->ht40
= ht40
;
6375 if (!freq_included(channels
, freq
)) {
6376 wpa_printf(MSG_DEBUG
, "P2P: Forced GO freq %d MHz not "
6380 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
6381 "frequency %d MHz", freq
);
6382 params
->freq
= freq
;
6383 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
6384 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
6385 wpa_s
->conf
->p2p_oper_channel
<= 11 &&
6386 freq_included(channels
,
6387 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
)) {
6388 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
6389 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
6390 "frequency %d MHz", params
->freq
);
6391 } else if ((wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
6392 wpa_s
->conf
->p2p_oper_reg_class
== 116 ||
6393 wpa_s
->conf
->p2p_oper_reg_class
== 117 ||
6394 wpa_s
->conf
->p2p_oper_reg_class
== 124 ||
6395 wpa_s
->conf
->p2p_oper_reg_class
== 126 ||
6396 wpa_s
->conf
->p2p_oper_reg_class
== 127) &&
6397 freq_included(channels
,
6398 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
)) {
6399 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
6400 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
6401 "frequency %d MHz", params
->freq
);
6402 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
6403 wpa_s
->best_overall_freq
> 0 &&
6404 p2p_supported_freq_go(wpa_s
->global
->p2p
,
6405 wpa_s
->best_overall_freq
) &&
6406 freq_included(channels
, wpa_s
->best_overall_freq
)) {
6407 params
->freq
= wpa_s
->best_overall_freq
;
6408 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
6409 "channel %d MHz", params
->freq
);
6410 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
6411 wpa_s
->best_24_freq
> 0 &&
6412 p2p_supported_freq_go(wpa_s
->global
->p2p
,
6413 wpa_s
->best_24_freq
) &&
6414 freq_included(channels
, wpa_s
->best_24_freq
)) {
6415 params
->freq
= wpa_s
->best_24_freq
;
6416 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
6417 "channel %d MHz", params
->freq
);
6418 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
6419 wpa_s
->best_5_freq
> 0 &&
6420 p2p_supported_freq_go(wpa_s
->global
->p2p
,
6421 wpa_s
->best_5_freq
) &&
6422 freq_included(channels
, wpa_s
->best_5_freq
)) {
6423 params
->freq
= wpa_s
->best_5_freq
;
6424 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
6425 "channel %d MHz", params
->freq
);
6426 } else if ((pref_freq
= p2p_get_pref_freq(wpa_s
->global
->p2p
,
6428 params
->freq
= pref_freq
;
6429 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz from preferred "
6430 "channels", params
->freq
);
6432 /* no preference, select some channel */
6433 if (wpas_p2p_select_freq_no_pref(wpa_s
, params
, channels
) < 0)
6437 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
,
6438 sizeof(struct wpa_used_freq_data
));
6442 num
= wpas_p2p_valid_oper_freqs(wpa_s
, freqs
,
6443 wpa_s
->num_multichan_concurrent
);
6445 cand_freq
= wpas_p2p_pick_best_used_freq(wpa_s
, freqs
, num
);
6447 /* First try the best used frequency if possible */
6448 if (!freq
&& cand_freq
> 0 && freq_included(channels
, cand_freq
)) {
6449 params
->freq
= cand_freq
;
6451 /* Try any of the used frequencies */
6452 for (i
= 0; i
< num
; i
++) {
6453 if (freq_included(channels
, freqs
[i
].freq
)) {
6454 wpa_printf(MSG_DEBUG
, "P2P: Force GO on a channel we are already using (%u MHz)",
6456 params
->freq
= freqs
[i
].freq
;
6462 if (wpas_p2p_num_unused_channels(wpa_s
) <= 0) {
6463 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on any of the channels we are already using");
6467 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
6471 for (i
= 0; i
< num
; i
++) {
6472 if (freqs
[i
].freq
== freq
)
6477 if (wpas_p2p_num_unused_channels(wpa_s
) <= 0) {
6479 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq
);
6483 wpa_printf(MSG_DEBUG
, "P2P: Use one of the free channels");
6493 static struct wpa_supplicant
*
6494 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
6497 struct wpa_supplicant
*group_wpa_s
;
6499 if (!wpas_p2p_create_iface(wpa_s
)) {
6500 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use same interface for group "
6502 wpa_s
->p2p_first_connection_timeout
= 0;
6506 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
6507 WPA_IF_P2P_CLIENT
) < 0) {
6508 wpa_msg_global(wpa_s
, MSG_ERROR
,
6509 "P2P: Failed to add group interface");
6512 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
6513 if (group_wpa_s
== NULL
) {
6514 wpa_msg_global(wpa_s
, MSG_ERROR
,
6515 "P2P: Failed to initialize group interface");
6516 wpas_p2p_remove_pending_group_interface(wpa_s
);
6520 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use separate group interface %s",
6521 group_wpa_s
->ifname
);
6522 group_wpa_s
->p2p_first_connection_timeout
= 0;
6528 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6529 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6530 * @persistent_group: Whether to create a persistent group
6531 * @freq: Frequency for the group or 0 to indicate no hardcoding
6532 * @ht40: Start GO with 40 MHz channel width
6533 * @vht: Start GO with VHT support
6534 * Returns: 0 on success, -1 on failure
6536 * This function creates a new P2P group with the local end as the Group Owner,
6537 * i.e., without using Group Owner Negotiation.
6539 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
6540 int freq
, int ht40
, int vht
)
6542 struct p2p_go_neg_results params
;
6544 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
6547 os_free(wpa_s
->global
->add_psk
);
6548 wpa_s
->global
->add_psk
= NULL
;
6550 /* Make sure we are not running find during connection establishment */
6551 wpa_printf(MSG_DEBUG
, "P2P: Stop any on-going P2P FIND");
6552 wpas_p2p_stop_find_oper(wpa_s
);
6554 freq
= wpas_p2p_select_go_freq(wpa_s
, freq
);
6558 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
, vht
, NULL
))
6561 !p2p_supported_freq_go(wpa_s
->global
->p2p
, params
.freq
)) {
6562 wpa_printf(MSG_DEBUG
, "P2P: The selected channel for GO "
6563 "(%u MHz) is not supported for P2P uses",
6567 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
6568 params
.persistent_group
= persistent_group
;
6570 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
6573 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
6579 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
6580 struct wpa_ssid
*params
, int addr_allocated
,
6583 struct wpa_ssid
*ssid
;
6585 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
6588 wpa_s
->p2p_last_4way_hs_fail
= NULL
;
6590 wpa_supplicant_ap_deinit(wpa_s
);
6592 ssid
= wpa_config_add_network(wpa_s
->conf
);
6595 wpa_config_set_network_defaults(ssid
);
6596 ssid
->temporary
= 1;
6597 ssid
->proto
= WPA_PROTO_RSN
;
6598 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
6599 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
6600 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
6601 ssid
->ssid
= os_malloc(params
->ssid_len
);
6602 if (ssid
->ssid
== NULL
) {
6603 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
6606 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
6607 ssid
->ssid_len
= params
->ssid_len
;
6608 ssid
->p2p_group
= 1;
6609 ssid
->export_keys
= 1;
6610 if (params
->psk_set
) {
6611 os_memcpy(ssid
->psk
, params
->psk
, 32);
6614 if (params
->passphrase
)
6615 ssid
->passphrase
= os_strdup(params
->passphrase
);
6617 wpa_s
->show_group_started
= 1;
6618 wpa_s
->p2p_in_invitation
= 1;
6619 wpa_s
->p2p_invite_go_freq
= freq
;
6621 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
6623 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT
, 0,
6624 wpas_p2p_group_formation_timeout
,
6625 wpa_s
->parent
, NULL
);
6626 wpa_supplicant_select_network(wpa_s
, ssid
);
6632 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
6633 struct wpa_ssid
*ssid
, int addr_allocated
,
6634 int force_freq
, int neg_freq
, int ht40
,
6635 int vht
, const struct p2p_channels
*channels
,
6636 int connection_timeout
)
6638 struct p2p_go_neg_results params
;
6641 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
6644 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
6645 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
6646 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
6651 os_free(wpa_s
->global
->add_psk
);
6652 wpa_s
->global
->add_psk
= NULL
;
6654 /* Make sure we are not running find during connection establishment */
6655 wpas_p2p_stop_find_oper(wpa_s
);
6657 wpa_s
->p2p_fallback_to_go_neg
= 0;
6659 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
6660 if (force_freq
> 0) {
6661 freq
= wpas_p2p_select_go_freq(wpa_s
, force_freq
);
6665 freq
= wpas_p2p_select_go_freq(wpa_s
, neg_freq
);
6667 (freq
> 0 && !freq_included(channels
, freq
)))
6673 (freq
> 0 && !freq_included(channels
, freq
)))
6677 if (ssid
->mode
== WPAS_MODE_INFRA
)
6678 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
, freq
);
6680 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
6683 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
, vht
, channels
))
6687 params
.psk_set
= ssid
->psk_set
;
6689 os_memcpy(params
.psk
, ssid
->psk
, sizeof(params
.psk
));
6690 if (ssid
->passphrase
) {
6691 if (os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
6692 wpa_printf(MSG_ERROR
, "P2P: Invalid passphrase in "
6693 "persistent group");
6696 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
6697 sizeof(params
.passphrase
));
6699 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
6700 params
.ssid_len
= ssid
->ssid_len
;
6701 params
.persistent_group
= 1;
6703 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
6707 p2p_channels_to_freqs(channels
, params
.freq_list
, P2P_MAX_CHANNELS
);
6709 wpa_s
->p2p_first_connection_timeout
= connection_timeout
;
6710 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
6716 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
6717 struct wpabuf
*proberesp_ies
)
6719 struct wpa_supplicant
*wpa_s
= ctx
;
6720 if (wpa_s
->ap_iface
) {
6721 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
6722 if (!(hapd
->conf
->p2p
& P2P_GROUP_OWNER
)) {
6723 wpabuf_free(beacon_ies
);
6724 wpabuf_free(proberesp_ies
);
6728 wpabuf_free(hapd
->p2p_beacon_ie
);
6729 hapd
->p2p_beacon_ie
= beacon_ies
;
6731 wpabuf_free(hapd
->p2p_probe_resp_ie
);
6732 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
6734 wpabuf_free(beacon_ies
);
6735 wpabuf_free(proberesp_ies
);
6737 wpa_supplicant_ap_update_beacon(wpa_s
);
6741 static void wpas_p2p_idle_update(void *ctx
, int idle
)
6743 struct wpa_supplicant
*wpa_s
= ctx
;
6744 if (!wpa_s
->ap_iface
)
6746 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
6748 if (wpa_s
->global
->p2p_fail_on_wps_complete
&&
6749 wpa_s
->p2p_in_provisioning
) {
6750 wpas_p2p_grpform_fail_after_wps(wpa_s
);
6753 wpas_p2p_set_group_idle_timeout(wpa_s
);
6755 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
6759 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
6760 struct wpa_ssid
*ssid
)
6762 struct p2p_group
*group
;
6763 struct p2p_group_config
*cfg
;
6765 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
6768 cfg
= os_zalloc(sizeof(*cfg
));
6772 if (ssid
->p2p_persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
6773 cfg
->persistent_group
= 2;
6774 else if (ssid
->p2p_persistent_group
)
6775 cfg
->persistent_group
= 1;
6776 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
6777 if (wpa_s
->max_stations
&&
6778 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
6779 cfg
->max_clients
= wpa_s
->max_stations
;
6781 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
6782 os_memcpy(cfg
->ssid
, ssid
->ssid
, ssid
->ssid_len
);
6783 cfg
->ssid_len
= ssid
->ssid_len
;
6784 cfg
->freq
= ssid
->frequency
;
6785 cfg
->cb_ctx
= wpa_s
;
6786 cfg
->ie_update
= wpas_p2p_ie_update
;
6787 cfg
->idle_update
= wpas_p2p_idle_update
;
6789 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
6792 if (ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
)
6793 p2p_group_notif_formation_done(group
);
6794 wpa_s
->p2p_group
= group
;
6799 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
6802 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
6804 if (!wpa_s
->p2p_in_provisioning
) {
6805 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
6806 "provisioning not in progress");
6810 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
6811 u8 go_dev_addr
[ETH_ALEN
];
6812 os_memcpy(go_dev_addr
, wpa_s
->bssid
, ETH_ALEN
);
6813 wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
6815 /* Clear any stored provisioning info */
6816 p2p_clear_provisioning_info(wpa_s
->global
->p2p
, go_dev_addr
);
6819 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
6821 wpa_s
->p2p_go_group_formation_completed
= 1;
6822 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
6824 * Use a separate timeout for initial data connection to
6825 * complete to allow the group to be removed automatically if
6826 * something goes wrong in this step before the P2P group idle
6827 * timeout mechanism is taken into use.
6829 wpa_dbg(wpa_s
, MSG_DEBUG
,
6830 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6831 P2P_MAX_INITIAL_CONN_WAIT
);
6832 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT
, 0,
6833 wpas_p2p_group_formation_timeout
,
6834 wpa_s
->parent
, NULL
);
6837 * Use a separate timeout for initial data connection to
6838 * complete to allow the group to be removed automatically if
6839 * the client does not complete data connection successfully.
6841 wpa_dbg(wpa_s
, MSG_DEBUG
,
6842 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6843 P2P_MAX_INITIAL_CONN_WAIT_GO
);
6844 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO
, 0,
6845 wpas_p2p_group_formation_timeout
,
6846 wpa_s
->parent
, NULL
);
6848 * Complete group formation on first successful data connection
6850 wpa_s
->p2p_go_group_formation_completed
= 0;
6852 if (wpa_s
->global
->p2p
)
6853 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
6854 wpas_group_formation_completed(wpa_s
, 1);
6858 void wpas_p2p_wps_failed(struct wpa_supplicant
*wpa_s
,
6859 struct wps_event_fail
*fail
)
6861 if (!wpa_s
->p2p_in_provisioning
) {
6862 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS fail event - P2P "
6863 "provisioning not in progress");
6867 if (wpa_s
->go_params
) {
6868 p2p_clear_provisioning_info(
6870 wpa_s
->go_params
->peer_device_addr
);
6873 wpas_notify_p2p_wps_failed(wpa_s
, fail
);
6875 if (wpa_s
== wpa_s
->global
->p2p_group_formation
) {
6877 * Allow some time for the failed WPS negotiation exchange to
6878 * complete, but remove the group since group formation cannot
6879 * succeed after provisioning failure.
6881 wpa_printf(MSG_DEBUG
, "P2P: WPS step failed during group formation - reject connection from timeout");
6882 wpa_s
->global
->p2p_fail_on_wps_complete
= 1;
6883 eloop_deplete_timeout(0, 50000,
6884 wpas_p2p_group_formation_timeout
,
6885 wpa_s
->parent
, NULL
);
6890 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant
*wpa_s
)
6892 if (!wpa_s
->global
->p2p_fail_on_wps_complete
||
6893 !wpa_s
->p2p_in_provisioning
)
6896 wpas_p2p_grpform_fail_after_wps(wpa_s
);
6902 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
6903 const char *config_method
,
6904 enum wpas_p2p_prov_disc_use use
,
6905 struct p2ps_provision
*p2ps_prov
)
6909 wpa_s
->global
->pending_p2ps_group
= 0;
6910 wpa_s
->p2p_fallback_to_go_neg
= 0;
6911 wpa_s
->pending_pd_use
= NORMAL_PD
;
6912 if (p2ps_prov
&& use
== WPAS_P2P_PD_FOR_ASP
) {
6913 p2ps_prov
->conncap
= p2ps_group_capability(
6914 wpa_s
, P2PS_SETUP_NONE
, p2ps_prov
->role
);
6915 wpa_printf(MSG_DEBUG
,
6916 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6917 __func__
, p2ps_prov
->conncap
,
6918 p2ps_prov
->adv_id
, p2ps_prov
->conncap
,
6919 p2ps_prov
->status
, p2ps_prov
->info
);
6922 } else if (os_strncmp(config_method
, "display", 7) == 0)
6923 config_methods
= WPS_CONFIG_DISPLAY
;
6924 else if (os_strncmp(config_method
, "keypad", 6) == 0)
6925 config_methods
= WPS_CONFIG_KEYPAD
;
6926 else if (os_strncmp(config_method
, "pbc", 3) == 0 ||
6927 os_strncmp(config_method
, "pushbutton", 10) == 0)
6928 config_methods
= WPS_CONFIG_PUSHBUTTON
;
6930 wpa_printf(MSG_DEBUG
, "P2P: Unknown config method");
6935 if (use
== WPAS_P2P_PD_AUTO
) {
6936 os_memcpy(wpa_s
->pending_join_dev_addr
, peer_addr
, ETH_ALEN
);
6937 wpa_s
->pending_pd_config_methods
= config_methods
;
6938 wpa_s
->p2p_auto_pd
= 1;
6939 wpa_s
->p2p_auto_join
= 0;
6940 wpa_s
->pending_pd_before_join
= 0;
6941 wpa_s
->auto_pd_scan_retry
= 0;
6942 wpas_p2p_stop_find(wpa_s
);
6943 wpa_s
->p2p_join_scan_count
= 0;
6944 os_get_reltime(&wpa_s
->p2p_auto_started
);
6945 wpa_printf(MSG_DEBUG
, "P2P: Auto PD started at %ld.%06ld",
6946 wpa_s
->p2p_auto_started
.sec
,
6947 wpa_s
->p2p_auto_started
.usec
);
6948 wpas_p2p_join_scan(wpa_s
, NULL
);
6952 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
) {
6957 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
, p2ps_prov
,
6958 config_methods
, use
== WPAS_P2P_PD_FOR_JOIN
,
6963 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
6966 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
6970 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
6972 if (!offchannel_pending_action_tx(wpa_s
))
6975 wpas_p2p_action_tx_clear(wpa_s
);
6977 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
6978 "operation request");
6979 offchannel_clear_pending_action_tx(wpa_s
);
6983 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
6984 enum p2p_discovery_type type
,
6985 unsigned int num_req_dev_types
, const u8
*req_dev_types
,
6986 const u8
*dev_id
, unsigned int search_delay
,
6987 u8 seek_cnt
, const char **seek_string
)
6989 wpas_p2p_clear_pending_action_tx(wpa_s
);
6990 wpa_s
->p2p_long_listen
= 0;
6992 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
||
6993 wpa_s
->p2p_in_provisioning
)
6996 wpa_supplicant_cancel_sched_scan(wpa_s
);
6998 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
,
6999 num_req_dev_types
, req_dev_types
, dev_id
,
7000 search_delay
, seek_cnt
, seek_string
);
7004 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant
*wpa_s
,
7005 struct wpa_scan_results
*scan_res
)
7007 wpa_printf(MSG_DEBUG
, "P2P: Ignore scan results");
7009 if (wpa_s
->p2p_scan_work
) {
7010 struct wpa_radio_work
*work
= wpa_s
->p2p_scan_work
;
7011 wpa_s
->p2p_scan_work
= NULL
;
7012 radio_work_done(work
);
7015 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
7019 * Indicate that results have been processed so that the P2P module can
7020 * continue pending tasks.
7022 p2p_scan_res_handled(wpa_s
->global
->p2p
);
7026 static void wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
)
7028 wpas_p2p_clear_pending_action_tx(wpa_s
);
7029 wpa_s
->p2p_long_listen
= 0;
7030 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
7031 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
7033 if (wpa_s
->global
->p2p
)
7034 p2p_stop_find(wpa_s
->global
->p2p
);
7036 if (wpa_s
->scan_res_handler
== wpas_p2p_scan_res_handler
) {
7037 wpa_printf(MSG_DEBUG
,
7038 "P2P: Do not consider the scan results after stop_find");
7039 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_ignore_search
;
7044 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
7046 wpas_p2p_stop_find_oper(wpa_s
);
7047 if (!wpa_s
->global
->pending_group_iface_for_p2ps
)
7048 wpas_p2p_remove_pending_group_interface(wpa_s
);
7052 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
7054 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
7055 wpa_s
->p2p_long_listen
= 0;
7059 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
7063 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
7066 wpa_supplicant_cancel_sched_scan(wpa_s
);
7067 wpas_p2p_clear_pending_action_tx(wpa_s
);
7071 * This is a request for unlimited Listen state. However, at
7072 * least for now, this is mapped to a Listen state for one
7077 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
7078 wpa_s
->p2p_long_listen
= 0;
7081 * Stop previous find/listen operation to avoid trying to request a new
7082 * remain-on-channel operation while the driver is still running the
7085 if (wpa_s
->global
->p2p
)
7086 p2p_stop_find(wpa_s
->global
->p2p
);
7088 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
7089 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
7090 wpa_s
->p2p_long_listen
= timeout
* 1000;
7091 eloop_register_timeout(timeout
, 0,
7092 wpas_p2p_long_listen_timeout
,
7100 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
7101 u8
*buf
, size_t len
, int p2p_group
)
7103 struct wpabuf
*p2p_ie
;
7106 if (wpa_s
->global
->p2p_disabled
)
7108 if (wpa_s
->conf
->p2p_disabled
)
7110 if (wpa_s
->global
->p2p
== NULL
)
7115 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
7116 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
7118 wpabuf_free(p2p_ie
);
7124 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
7125 const u8
*dst
, const u8
*bssid
,
7126 const u8
*ie
, size_t ie_len
, int ssi_signal
)
7128 if (wpa_s
->global
->p2p_disabled
)
7130 if (wpa_s
->global
->p2p
== NULL
)
7133 switch (p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, dst
, bssid
,
7135 case P2P_PREQ_NOT_P2P
:
7136 wpas_notify_preq(wpa_s
, addr
, dst
, bssid
, ie
, ie_len
,
7139 case P2P_PREQ_MALFORMED
:
7140 case P2P_PREQ_NOT_LISTEN
:
7141 case P2P_PREQ_NOT_PROCESSED
:
7142 default: /* make gcc happy */
7144 case P2P_PREQ_PROCESSED
:
7150 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
7151 const u8
*sa
, const u8
*bssid
,
7152 u8 category
, const u8
*data
, size_t len
, int freq
)
7154 if (wpa_s
->global
->p2p_disabled
)
7156 if (wpa_s
->global
->p2p
== NULL
)
7159 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
7164 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
7166 if (wpa_s
->global
->p2p_disabled
)
7168 if (wpa_s
->global
->p2p
== NULL
)
7171 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
7175 static void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
7177 p2p_group_deinit(wpa_s
->p2p_group
);
7178 wpa_s
->p2p_group
= NULL
;
7180 wpa_s
->ap_configured_cb
= NULL
;
7181 wpa_s
->ap_configured_cb_ctx
= NULL
;
7182 wpa_s
->ap_configured_cb_data
= NULL
;
7183 wpa_s
->connect_without_scan
= NULL
;
7187 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
7189 wpa_s
->p2p_long_listen
= 0;
7191 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
7194 return p2p_reject(wpa_s
->global
->p2p
, addr
);
7198 /* Invite to reinvoke a persistent group */
7199 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
7200 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
, int freq
,
7201 int ht40
, int vht
, int pref_freq
)
7203 enum p2p_invite_role role
;
7207 int no_pref_freq_given
= pref_freq
== 0;
7209 wpa_s
->global
->p2p_invite_group
= NULL
;
7211 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
7213 os_memset(wpa_s
->p2p_auth_invite
, 0, ETH_ALEN
);
7215 wpa_s
->p2p_persistent_go_freq
= freq
;
7216 wpa_s
->p2p_go_ht40
= !!ht40
;
7217 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
7218 role
= P2P_INVITE_ROLE_GO
;
7219 if (peer_addr
== NULL
) {
7220 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
7221 "address in invitation command");
7224 if (wpas_p2p_create_iface(wpa_s
)) {
7225 if (wpas_p2p_add_group_interface(wpa_s
,
7226 WPA_IF_P2P_GO
) < 0) {
7227 wpa_printf(MSG_ERROR
, "P2P: Failed to "
7228 "allocate a new interface for the "
7232 bssid
= wpa_s
->pending_interface_addr
;
7234 bssid
= wpa_s
->own_addr
;
7236 role
= P2P_INVITE_ROLE_CLIENT
;
7237 peer_addr
= ssid
->bssid
;
7239 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
7241 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
7242 role
== P2P_INVITE_ROLE_GO
);
7246 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
7249 if (wpa_s
->parent
->conf
->p2p_ignore_shared_freq
&&
7250 no_pref_freq_given
&& pref_freq
> 0 &&
7251 wpa_s
->num_multichan_concurrent
> 1 &&
7252 wpas_p2p_num_unused_channels(wpa_s
) > 0) {
7253 wpa_printf(MSG_DEBUG
, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7259 * Stop any find/listen operations before invitation and possibly
7260 * connection establishment.
7262 wpas_p2p_stop_find_oper(wpa_s
);
7264 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
7265 ssid
->ssid
, ssid
->ssid_len
, force_freq
, go_dev_addr
,
7270 /* Invite to join an active group */
7271 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
7272 const u8
*peer_addr
, const u8
*go_dev_addr
)
7274 struct wpa_global
*global
= wpa_s
->global
;
7275 enum p2p_invite_role role
;
7277 struct wpa_ssid
*ssid
;
7279 int freq
= 0, force_freq
= 0, pref_freq
= 0;
7282 wpa_s
->p2p_persistent_go_freq
= 0;
7283 wpa_s
->p2p_go_ht40
= 0;
7284 wpa_s
->p2p_go_vht
= 0;
7286 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
7287 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
7290 if (wpa_s
== NULL
) {
7291 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
7295 ssid
= wpa_s
->current_ssid
;
7297 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
7302 wpa_s
->global
->p2p_invite_group
= wpa_s
;
7303 persistent
= ssid
->p2p_persistent_group
&&
7304 wpas_p2p_get_persistent(wpa_s
->parent
, peer_addr
,
7305 ssid
->ssid
, ssid
->ssid_len
);
7307 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
7308 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
7309 bssid
= wpa_s
->own_addr
;
7310 if (go_dev_addr
== NULL
)
7311 go_dev_addr
= wpa_s
->global
->p2p_dev_addr
;
7312 freq
= ssid
->frequency
;
7314 role
= P2P_INVITE_ROLE_CLIENT
;
7315 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
7316 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
7317 "invite to current group");
7320 bssid
= wpa_s
->bssid
;
7321 if (go_dev_addr
== NULL
&&
7322 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
7323 go_dev_addr
= wpa_s
->go_dev_addr
;
7324 freq
= wpa_s
->current_bss
? wpa_s
->current_bss
->freq
:
7325 (int) wpa_s
->assoc_freq
;
7327 wpa_s
->parent
->pending_invite_ssid_id
= -1;
7329 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
7332 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
7333 role
== P2P_INVITE_ROLE_ACTIVE_GO
);
7336 wpas_p2p_set_own_freq_preference(wpa_s
, force_freq
);
7338 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
7339 ssid
->ssid
, ssid
->ssid_len
, force_freq
,
7340 go_dev_addr
, persistent
, pref_freq
, -1);
7344 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
7346 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
7347 u8 go_dev_addr
[ETH_ALEN
];
7348 int network_id
= -1;
7354 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
) {
7355 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
7356 wpa_s
->parent
, NULL
);
7359 if (!wpa_s
->show_group_started
|| !ssid
)
7362 wpa_s
->show_group_started
= 0;
7364 os_memset(go_dev_addr
, 0, ETH_ALEN
);
7365 if (ssid
->bssid_set
)
7366 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
7367 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
7369 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
7371 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
7372 wpa_s
->global
->p2p_group_formation
= NULL
;
7374 freq
= wpa_s
->current_bss
? wpa_s
->current_bss
->freq
:
7375 (int) wpa_s
->assoc_freq
;
7378 if (wpa_sm_get_p2p_ip_addr(wpa_s
->wpa
, ip
) == 0) {
7381 res
= os_snprintf(ip_addr
, sizeof(ip_addr
),
7382 " ip_addr=%u.%u.%u.%u "
7383 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7384 ip
[0], ip
[1], ip
[2], ip
[3],
7385 ip
[4], ip
[5], ip
[6], ip
[7],
7386 ip
[8], ip
[9], ip
[10], ip
[11]);
7387 if (os_snprintf_error(sizeof(ip_addr
), res
))
7391 wpas_p2p_group_started(wpa_s
, 0, ssid
, freq
,
7392 ssid
->passphrase
== NULL
&& ssid
->psk_set
?
7394 ssid
->passphrase
, go_dev_addr
, persistent
,
7398 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
7401 network_id
= ssid
->id
;
7402 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 1);
7406 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
7407 u32 interval1
, u32 duration2
, u32 interval2
)
7411 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
7414 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
7415 wpa_s
->current_ssid
== NULL
||
7416 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
7419 ret
= p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
7420 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
7421 duration1
, interval1
, duration2
, interval2
);
7423 wpa_s
->waiting_presence_resp
= 1;
7429 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
7430 unsigned int interval
)
7432 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
7435 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
7439 static int wpas_p2p_is_client(struct wpa_supplicant
*wpa_s
)
7441 if (wpa_s
->current_ssid
== NULL
) {
7443 * current_ssid can be cleared when P2P client interface gets
7444 * disconnected, so assume this interface was used as P2P
7449 return wpa_s
->current_ssid
->p2p_group
&&
7450 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
;
7454 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
7456 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
7458 if (wpa_s
->conf
->p2p_group_idle
== 0 && !wpas_p2p_is_client(wpa_s
)) {
7459 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
7464 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
7466 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_IDLE_TIMEOUT
);
7470 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
7474 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
7475 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
7477 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
7480 timeout
= wpa_s
->conf
->p2p_group_idle
;
7481 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
&&
7482 (timeout
== 0 || timeout
> P2P_MAX_CLIENT_IDLE
))
7483 timeout
= P2P_MAX_CLIENT_IDLE
;
7489 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
)
7490 timeout
= 0; /* special client mode no-timeout */
7495 if (wpa_s
->p2p_in_provisioning
) {
7497 * Use the normal group formation timeout during the
7498 * provisioning phase to avoid terminating this process too
7499 * early due to group idle timeout.
7501 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
7502 "during provisioning");
7506 if (wpa_s
->show_group_started
) {
7508 * Use the normal group formation timeout between the end of
7509 * the provisioning phase and completion of 4-way handshake to
7510 * avoid terminating this process too early due to group idle
7513 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
7514 "while waiting for initial 4-way handshake to "
7519 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
7521 eloop_register_timeout(timeout
, 0, wpas_p2p_group_idle_timeout
,
7526 /* Returns 1 if the interface was removed */
7527 int wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
7528 u16 reason_code
, const u8
*ie
, size_t ie_len
,
7529 int locally_generated
)
7531 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
7534 if (!locally_generated
)
7535 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
7538 if (reason_code
== WLAN_REASON_DEAUTH_LEAVING
&& !locally_generated
&&
7539 wpa_s
->current_ssid
&&
7540 wpa_s
->current_ssid
->p2p_group
&&
7541 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
) {
7542 wpa_printf(MSG_DEBUG
, "P2P: GO indicated that the P2P Group "
7543 "session is ending");
7544 if (wpas_p2p_group_delete(wpa_s
,
7545 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
)
7554 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
7555 u16 reason_code
, const u8
*ie
, size_t ie_len
,
7556 int locally_generated
)
7558 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
7561 if (!locally_generated
)
7562 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
7567 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
7569 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
7574 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
7577 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
7578 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
7580 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
)
7581 p2p_set_pri_dev_type(p2p
, wpa_s
->conf
->device_type
);
7584 (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_CONFIG_METHODS
))
7585 p2p_set_config_methods(p2p
, wpa_s
->wps
->config_methods
);
7587 if (wpa_s
->wps
&& (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_UUID
))
7588 p2p_set_uuid(p2p
, wpa_s
->wps
->uuid
);
7590 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_WPS_STRING
) {
7591 p2p_set_manufacturer(p2p
, wpa_s
->conf
->manufacturer
);
7592 p2p_set_model_name(p2p
, wpa_s
->conf
->model_name
);
7593 p2p_set_model_number(p2p
, wpa_s
->conf
->model_number
);
7594 p2p_set_serial_number(p2p
, wpa_s
->conf
->serial_number
);
7597 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
)
7598 p2p_set_sec_dev_types(p2p
,
7599 (void *) wpa_s
->conf
->sec_device_type
,
7600 wpa_s
->conf
->num_sec_device_types
);
7602 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_VENDOR_EXTENSION
) {
7604 p2p_remove_wps_vendor_extensions(p2p
);
7605 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
7606 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
7608 p2p_add_wps_vendor_extension(
7609 p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
7613 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
7614 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
7616 country
[0] = wpa_s
->conf
->country
[0];
7617 country
[1] = wpa_s
->conf
->country
[1];
7619 p2p_set_country(p2p
, country
);
7622 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
7623 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
7624 wpa_s
->conf
->p2p_ssid_postfix
?
7625 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
7629 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
7630 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
7632 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_LISTEN_CHANNEL
) {
7633 u8 reg_class
, channel
;
7638 if (wpa_s
->conf
->p2p_listen_reg_class
&&
7639 wpa_s
->conf
->p2p_listen_channel
) {
7640 reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
7641 channel
= wpa_s
->conf
->p2p_listen_channel
;
7646 * Pick one of the social channels randomly as the
7649 if (os_get_random((u8
*) &r
, sizeof(r
)) < 0)
7652 channel
= 1 + (r
% 3) * 5;
7655 ret
= p2p_set_listen_channel(p2p
, reg_class
, channel
,
7658 wpa_printf(MSG_ERROR
, "P2P: Own listen channel update "
7661 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_OPER_CHANNEL
) {
7662 u8 op_reg_class
, op_channel
, cfg_op_channel
;
7665 if (wpa_s
->conf
->p2p_oper_reg_class
&&
7666 wpa_s
->conf
->p2p_oper_channel
) {
7667 op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
7668 op_channel
= wpa_s
->conf
->p2p_oper_channel
;
7673 * Use random operation channel from (1, 6, 11)
7674 *if no other preference is indicated.
7676 if (os_get_random((u8
*) &r
, sizeof(r
)) < 0)
7679 op_channel
= 1 + (r
% 3) * 5;
7682 ret
= p2p_set_oper_channel(p2p
, op_reg_class
, op_channel
,
7685 wpa_printf(MSG_ERROR
, "P2P: Own oper channel update "
7689 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_PREF_CHAN
) {
7690 if (p2p_set_pref_chan(p2p
, wpa_s
->conf
->num_p2p_pref_chan
,
7691 wpa_s
->conf
->p2p_pref_chan
) < 0) {
7692 wpa_printf(MSG_ERROR
, "P2P: Preferred channel list "
7696 if (p2p_set_no_go_freq(p2p
, &wpa_s
->conf
->p2p_no_go_freq
) < 0) {
7697 wpa_printf(MSG_ERROR
, "P2P: No GO channel list "
7702 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_PASSPHRASE_LEN
)
7703 p2p_set_passphrase_len(p2p
, wpa_s
->conf
->p2p_passphrase_len
);
7707 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
7710 if (!wpa_s
->ap_iface
)
7712 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
7717 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
7719 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
7722 wpa_s
->global
->cross_connection
= enabled
;
7723 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
7726 struct wpa_supplicant
*iface
;
7728 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
7730 if (iface
->cross_connect_enabled
== 0)
7733 iface
->cross_connect_enabled
= 0;
7734 iface
->cross_connect_in_use
= 0;
7735 wpa_msg_global(iface
->parent
, MSG_INFO
,
7736 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
7738 iface
->cross_connect_uplink
);
7746 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
7748 struct wpa_supplicant
*iface
;
7750 if (!uplink
->global
->cross_connection
)
7753 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
7754 if (!iface
->cross_connect_enabled
)
7756 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
7759 if (iface
->ap_iface
== NULL
)
7761 if (iface
->cross_connect_in_use
)
7764 iface
->cross_connect_in_use
= 1;
7765 wpa_msg_global(iface
->parent
, MSG_INFO
,
7766 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
7767 iface
->ifname
, iface
->cross_connect_uplink
);
7772 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
7774 struct wpa_supplicant
*iface
;
7776 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
7777 if (!iface
->cross_connect_enabled
)
7779 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
7782 if (!iface
->cross_connect_in_use
)
7785 wpa_msg_global(iface
->parent
, MSG_INFO
,
7786 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
7787 iface
->ifname
, iface
->cross_connect_uplink
);
7788 iface
->cross_connect_in_use
= 0;
7793 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
7795 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
7796 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
7797 wpa_s
->cross_connect_disallowed
)
7798 wpas_p2p_disable_cross_connect(wpa_s
);
7800 wpas_p2p_enable_cross_connect(wpa_s
);
7801 if (!wpa_s
->ap_iface
&&
7802 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
7803 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
7807 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
7809 wpas_p2p_disable_cross_connect(wpa_s
);
7810 if (!wpa_s
->ap_iface
&&
7811 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout
,
7813 wpas_p2p_set_group_idle_timeout(wpa_s
);
7817 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
7819 struct wpa_supplicant
*iface
;
7821 if (!wpa_s
->global
->cross_connection
)
7824 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
7827 if (iface
->drv_flags
&
7828 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
7830 if ((iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
) &&
7831 iface
!= wpa_s
->parent
)
7834 wpa_s
->cross_connect_enabled
= 1;
7835 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
7836 sizeof(wpa_s
->cross_connect_uplink
));
7837 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
7838 "%s to %s whenever uplink is available",
7839 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
7841 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
7842 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
7843 iface
->cross_connect_disallowed
||
7844 iface
->wpa_state
!= WPA_COMPLETED
)
7847 wpa_s
->cross_connect_in_use
= 1;
7848 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
7849 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
7850 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
7856 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
7858 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
7859 !wpa_s
->p2p_in_provisioning
)
7860 return 0; /* not P2P client operation */
7862 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
7864 if (wpa_s
!= wpa_s
->parent
)
7865 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
7866 wpas_p2p_group_formation_failed(wpa_s
);
7871 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx
, void *timeout_ctx
)
7873 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
7874 wpas_p2p_notif_pbc_overlap(wpa_s
);
7878 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
7880 struct p2p_channels chan
, cli_chan
;
7881 struct wpa_supplicant
*ifs
;
7883 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
7886 os_memset(&chan
, 0, sizeof(chan
));
7887 os_memset(&cli_chan
, 0, sizeof(cli_chan
));
7888 if (wpas_p2p_setup_channels(wpa_s
, &chan
, &cli_chan
)) {
7889 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
7894 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
, &cli_chan
);
7896 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
7898 if (!ifs
->current_ssid
||
7899 !ifs
->current_ssid
->p2p_group
||
7900 (ifs
->current_ssid
->mode
!= WPAS_MODE_P2P_GO
&&
7901 ifs
->current_ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
))
7903 freq
= ifs
->current_ssid
->frequency
;
7904 if (freq_included(&chan
, freq
)) {
7905 wpa_dbg(ifs
, MSG_DEBUG
,
7906 "P2P GO operating frequency %d MHz in valid range",
7911 wpa_dbg(ifs
, MSG_DEBUG
,
7912 "P2P GO operating in invalid frequency %d MHz", freq
);
7913 /* TODO: Consider using CSA or removing the group within
7915 wpa_msg(ifs
, MSG_INFO
, P2P_EVENT_REMOVE_AND_REFORM_GROUP
);
7920 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant
*wpa_s
,
7921 struct wpa_scan_results
*scan_res
)
7923 wpa_printf(MSG_DEBUG
, "P2P: Ignore scan results");
7927 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
7929 struct wpa_global
*global
= wpa_s
->global
;
7933 if (global
->p2p
== NULL
)
7936 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
7938 if (wpa_s
->pending_interface_name
[0] &&
7939 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
7942 peer
= p2p_get_go_neg_peer(global
->p2p
);
7944 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
7945 MACSTR
, MAC2STR(peer
));
7946 p2p_unauthorize(global
->p2p
, peer
);
7950 if (wpa_s
->scan_res_handler
== wpas_p2p_scan_res_join
) {
7951 wpa_printf(MSG_DEBUG
, "P2P: Stop pending scan for join");
7952 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_ignore
;
7956 if (wpa_s
->pending_pd_before_join
) {
7957 wpa_printf(MSG_DEBUG
, "P2P: Stop pending PD before join");
7958 wpa_s
->pending_pd_before_join
= 0;
7962 wpas_p2p_stop_find(wpa_s
);
7964 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
7965 if (wpa_s
== global
->p2p_group_formation
&&
7966 (wpa_s
->p2p_in_provisioning
||
7967 wpa_s
->parent
->pending_interface_type
==
7968 WPA_IF_P2P_CLIENT
)) {
7969 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
7970 "formation found - cancelling",
7973 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
7974 wpa_s
->parent
, NULL
);
7975 if (wpa_s
->p2p_in_provisioning
) {
7976 wpas_group_formation_completed(wpa_s
, 0);
7979 wpas_p2p_group_delete(wpa_s
,
7980 P2P_GROUP_REMOVAL_REQUESTED
);
7982 } else if (wpa_s
->p2p_in_invitation
) {
7983 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in invitation found - cancelling",
7986 wpas_p2p_group_formation_failed(wpa_s
);
7991 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
7999 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
8001 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
8004 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
8005 "being available anymore");
8006 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_UNAVAILABLE
);
8010 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
8011 int freq_24
, int freq_5
, int freq_overall
)
8013 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
8016 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
8020 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
8023 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
8028 if (hwaddr_aton(addr
, peer
))
8031 return p2p_unauthorize(p2p
, peer
);
8036 * wpas_p2p_disconnect - Disconnect from a P2P Group
8037 * @wpa_s: Pointer to wpa_supplicant data
8038 * Returns: 0 on success, -1 on failure
8040 * This can be used to disconnect from a group in which the local end is a P2P
8041 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8042 * virtual network interface was created for this group, that interface will be
8043 * removed. Otherwise, only the configured P2P group network will be removed
8044 * from the interface.
8046 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
8052 return wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_REQUESTED
) < 0 ?
8057 int wpas_p2p_in_progress(struct wpa_supplicant
*wpa_s
)
8061 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
8064 ret
= p2p_in_progress(wpa_s
->global
->p2p
);
8067 * Check whether there is an ongoing WPS provisioning step (or
8068 * other parts of group formation) on another interface since
8069 * p2p_in_progress() does not report this to avoid issues for
8070 * scans during such provisioning step.
8072 if (wpa_s
->global
->p2p_group_formation
&&
8073 wpa_s
->global
->p2p_group_formation
!= wpa_s
) {
8074 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Another interface (%s) "
8075 "in group formation",
8076 wpa_s
->global
->p2p_group_formation
->ifname
);
8081 if (!ret
&& wpa_s
->global
->p2p_go_wait_client
.sec
) {
8082 struct os_reltime now
;
8083 os_get_reltime(&now
);
8084 if (os_reltime_expired(&now
, &wpa_s
->global
->p2p_go_wait_client
,
8085 P2P_MAX_INITIAL_CONN_WAIT_GO
)) {
8086 /* Wait for the first client has expired */
8087 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
8089 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Waiting for initial client connection during group formation");
8098 void wpas_p2p_network_removed(struct wpa_supplicant
*wpa_s
,
8099 struct wpa_ssid
*ssid
)
8101 if (wpa_s
->p2p_in_provisioning
&& ssid
->p2p_group
&&
8102 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
8103 wpa_s
->parent
, NULL
) > 0) {
8105 * Remove the network by scheduling the group formation
8106 * timeout to happen immediately. The teardown code
8107 * needs to be scheduled to run asynch later so that we
8108 * don't delete data from under ourselves unexpectedly.
8109 * Calling wpas_p2p_group_formation_timeout directly
8110 * causes a series of crashes in WPS failure scenarios.
8112 wpa_printf(MSG_DEBUG
, "P2P: Canceled group formation due to "
8113 "P2P group network getting removed");
8114 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
8115 wpa_s
->parent
, NULL
);
8120 struct wpa_ssid
* wpas_p2p_get_persistent(struct wpa_supplicant
*wpa_s
,
8121 const u8
*addr
, const u8
*ssid
,
8127 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
8128 if (s
->disabled
!= 2)
8131 (ssid_len
!= s
->ssid_len
||
8132 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0))
8135 if (s
->mode
== WPAS_MODE_P2P_GO
)
8139 if (os_memcmp(s
->bssid
, addr
, ETH_ALEN
) == 0)
8140 return s
; /* peer is GO in the persistent group */
8141 if (s
->mode
!= WPAS_MODE_P2P_GO
|| s
->p2p_client_list
== NULL
)
8143 for (i
= 0; i
< s
->num_p2p_clients
; i
++) {
8144 if (os_memcmp(s
->p2p_client_list
+ i
* 2 * ETH_ALEN
,
8145 addr
, ETH_ALEN
) == 0)
8146 return s
; /* peer is P2P client in persistent
8155 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant
*wpa_s
,
8158 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
8159 wpa_s
->parent
, NULL
) > 0) {
8161 * This can happen if WPS provisioning step is not terminated
8162 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8163 * peer was able to connect, there is no need to time out group
8164 * formation after this, though. In addition, this is used with
8165 * the initial connection wait on the GO as a separate formation
8166 * timeout and as such, expected to be hit after the initial WPS
8167 * provisioning step.
8169 wpa_printf(MSG_DEBUG
, "P2P: Canceled P2P group formation timeout on data connection");
8171 if (!wpa_s
->p2p_go_group_formation_completed
&&
8172 !wpa_s
->group_formation_reported
) {
8174 * GO has not yet notified group formation success since
8175 * the WPS step was not completed cleanly. Do that
8176 * notification now since the P2P Client was able to
8177 * connect and as such, must have received the
8178 * credential from the WPS step.
8180 if (wpa_s
->global
->p2p
)
8181 p2p_wps_success_cb(wpa_s
->global
->p2p
, addr
);
8182 wpas_group_formation_completed(wpa_s
, 1);
8185 if (!wpa_s
->p2p_go_group_formation_completed
) {
8186 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Marking group formation completed on GO on first data connection");
8187 wpa_s
->p2p_go_group_formation_completed
= 1;
8188 wpa_s
->global
->p2p_group_formation
= NULL
;
8189 wpa_s
->p2p_in_provisioning
= 0;
8190 wpa_s
->p2p_in_invitation
= 0;
8192 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
8195 wpas_p2p_add_persistent_group_client(wpa_s
, addr
);
8199 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
8202 struct wpa_supplicant
*group
= wpa_s
;
8205 if (wpa_s
->global
->p2p_group_formation
)
8206 group
= wpa_s
->global
->p2p_group_formation
;
8207 wpa_s
= wpa_s
->parent
;
8208 offchannel_send_action_done(wpa_s
);
8210 ret
= wpas_p2p_group_delete(group
, P2P_GROUP_REMOVAL_SILENT
);
8211 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Fall back to GO Negotiation");
8212 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
, wpa_s
->p2p_pin
,
8213 wpa_s
->p2p_wps_method
, wpa_s
->p2p_persistent_group
, 0,
8214 0, 0, wpa_s
->p2p_go_intent
, wpa_s
->p2p_connect_freq
,
8215 wpa_s
->p2p_persistent_id
,
8216 wpa_s
->p2p_pd_before_go_neg
,
8223 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant
*wpa_s
)
8227 if (!wpa_s
->p2p_fallback_to_go_neg
||
8228 wpa_s
->p2p_in_provisioning
<= 5)
8231 if (wpas_p2p_peer_go(wpa_s
, wpa_s
->pending_join_dev_addr
) > 0)
8232 return 0; /* peer operating as a GO */
8234 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: GO not found for p2p_connect-auto - "
8235 "fallback to GO Negotiation");
8236 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_FALLBACK_TO_GO_NEG
8237 "reason=GO-not-found");
8238 res
= wpas_p2p_fallback_to_go_neg(wpa_s
, 1);
8240 return res
== 1 ? 2 : 1;
8244 unsigned int wpas_p2p_search_delay(struct wpa_supplicant
*wpa_s
)
8246 struct wpa_supplicant
*ifs
;
8248 if (wpa_s
->wpa_state
> WPA_SCANNING
) {
8249 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search delay due to "
8250 "concurrent operation",
8251 wpa_s
->conf
->p2p_search_delay
);
8252 return wpa_s
->conf
->p2p_search_delay
;
8255 dl_list_for_each(ifs
, &wpa_s
->radio
->ifaces
, struct wpa_supplicant
,
8257 if (ifs
!= wpa_s
&& ifs
->wpa_state
> WPA_SCANNING
) {
8258 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search "
8259 "delay due to concurrent operation on "
8261 wpa_s
->conf
->p2p_search_delay
,
8263 return wpa_s
->conf
->p2p_search_delay
;
8271 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant
*wpa_s
,
8272 struct wpa_ssid
*s
, const u8
*addr
,
8275 struct psk_list_entry
*psk
, *tmp
;
8278 dl_list_for_each_safe(psk
, tmp
, &s
->psk_list
, struct psk_list_entry
,
8280 if ((iface_addr
&& !psk
->p2p
&&
8281 os_memcmp(addr
, psk
->addr
, ETH_ALEN
) == 0) ||
8282 (!iface_addr
&& psk
->p2p
&&
8283 os_memcmp(addr
, psk
->addr
, ETH_ALEN
) == 0)) {
8284 wpa_dbg(wpa_s
, MSG_DEBUG
,
8285 "P2P: Remove persistent group PSK list entry for "
8287 MAC2STR(psk
->addr
), psk
->p2p
);
8288 dl_list_del(&psk
->list
);
8298 void wpas_p2p_new_psk_cb(struct wpa_supplicant
*wpa_s
, const u8
*mac_addr
,
8299 const u8
*p2p_dev_addr
,
8300 const u8
*psk
, size_t psk_len
)
8302 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
8303 struct wpa_ssid
*persistent
;
8304 struct psk_list_entry
*p
, *last
;
8306 if (psk_len
!= sizeof(p
->psk
))
8310 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: New PSK for addr=" MACSTR
8311 " p2p_dev_addr=" MACSTR
,
8312 MAC2STR(mac_addr
), MAC2STR(p2p_dev_addr
));
8313 if (is_zero_ether_addr(p2p_dev_addr
))
8314 p2p_dev_addr
= NULL
;
8316 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: New PSK for addr=" MACSTR
,
8320 if (ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
8321 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: new_psk_cb during group formation");
8322 /* To be added to persistent group once created */
8323 if (wpa_s
->global
->add_psk
== NULL
) {
8324 wpa_s
->global
->add_psk
= os_zalloc(sizeof(*p
));
8325 if (wpa_s
->global
->add_psk
== NULL
)
8328 p
= wpa_s
->global
->add_psk
;
8331 os_memcpy(p
->addr
, p2p_dev_addr
, ETH_ALEN
);
8334 os_memcpy(p
->addr
, mac_addr
, ETH_ALEN
);
8336 os_memcpy(p
->psk
, psk
, psk_len
);
8340 if (ssid
->mode
!= WPAS_MODE_P2P_GO
|| !ssid
->p2p_persistent_group
) {
8341 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Ignore new_psk_cb on not-persistent GO");
8345 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, NULL
, ssid
->ssid
,
8348 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not find persistent group information to store the new PSK");
8352 p
= os_zalloc(sizeof(*p
));
8357 os_memcpy(p
->addr
, p2p_dev_addr
, ETH_ALEN
);
8360 os_memcpy(p
->addr
, mac_addr
, ETH_ALEN
);
8362 os_memcpy(p
->psk
, psk
, psk_len
);
8364 if (dl_list_len(&persistent
->psk_list
) > P2P_MAX_STORED_CLIENTS
&&
8365 (last
= dl_list_last(&persistent
->psk_list
,
8366 struct psk_list_entry
, list
))) {
8367 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove oldest PSK entry for "
8368 MACSTR
" (p2p=%u) to make room for a new one",
8369 MAC2STR(last
->addr
), last
->p2p
);
8370 dl_list_del(&last
->list
);
8374 wpas_p2p_remove_psk_entry(wpa_s
->parent
, persistent
,
8375 p2p_dev_addr
? p2p_dev_addr
: mac_addr
,
8376 p2p_dev_addr
== NULL
);
8378 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add new PSK for p2p_dev_addr="
8379 MACSTR
, MAC2STR(p2p_dev_addr
));
8381 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add new PSK for addr=" MACSTR
,
8384 dl_list_add(&persistent
->psk_list
, &p
->list
);
8386 if (wpa_s
->parent
->conf
->update_config
&&
8387 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
8388 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
8392 static void wpas_p2p_remove_psk(struct wpa_supplicant
*wpa_s
,
8393 struct wpa_ssid
*s
, const u8
*addr
,
8398 res
= wpas_p2p_remove_psk_entry(wpa_s
, s
, addr
, iface_addr
);
8399 if (res
> 0 && wpa_s
->conf
->update_config
&&
8400 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
))
8401 wpa_dbg(wpa_s
, MSG_DEBUG
,
8402 "P2P: Failed to update configuration");
8406 static void wpas_p2p_remove_client_go(struct wpa_supplicant
*wpa_s
,
8407 const u8
*peer
, int iface_addr
)
8409 struct hostapd_data
*hapd
;
8410 struct hostapd_wpa_psk
*psk
, *prev
, *rem
;
8411 struct sta_info
*sta
;
8413 if (wpa_s
->ap_iface
== NULL
|| wpa_s
->current_ssid
== NULL
||
8414 wpa_s
->current_ssid
->mode
!= WPAS_MODE_P2P_GO
)
8417 /* Remove per-station PSK entry */
8418 hapd
= wpa_s
->ap_iface
->bss
[0];
8420 psk
= hapd
->conf
->ssid
.wpa_psk
;
8422 if ((iface_addr
&& os_memcmp(peer
, psk
->addr
, ETH_ALEN
) == 0) ||
8424 os_memcmp(peer
, psk
->p2p_dev_addr
, ETH_ALEN
) == 0)) {
8425 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove operating group PSK entry for "
8426 MACSTR
" iface_addr=%d",
8427 MAC2STR(peer
), iface_addr
);
8429 prev
->next
= psk
->next
;
8431 hapd
->conf
->ssid
.wpa_psk
= psk
->next
;
8441 /* Disconnect from group */
8443 sta
= ap_get_sta(hapd
, peer
);
8445 sta
= ap_get_sta_p2p(hapd
, peer
);
8447 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Disconnect peer " MACSTR
8448 " (iface_addr=%d) from group",
8449 MAC2STR(peer
), iface_addr
);
8450 hostapd_drv_sta_deauth(hapd
, sta
->addr
,
8451 WLAN_REASON_DEAUTH_LEAVING
);
8452 ap_sta_deauthenticate(hapd
, sta
, WLAN_REASON_DEAUTH_LEAVING
);
8457 void wpas_p2p_remove_client(struct wpa_supplicant
*wpa_s
, const u8
*peer
,
8461 struct wpa_supplicant
*w
;
8463 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove client " MACSTR
, MAC2STR(peer
));
8465 /* Remove from any persistent group */
8466 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
8467 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
8470 wpas_remove_persistent_peer(wpa_s
, s
, peer
, 0);
8471 wpas_p2p_remove_psk(wpa_s
->parent
, s
, peer
, iface_addr
);
8474 /* Remove from any operating group */
8475 for (w
= wpa_s
->global
->ifaces
; w
; w
= w
->next
)
8476 wpas_p2p_remove_client_go(w
, peer
, iface_addr
);
8480 static void wpas_p2p_psk_failure_removal(void *eloop_ctx
, void *timeout_ctx
)
8482 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
8483 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_PSK_FAILURE
);
8487 static void wpas_p2p_group_freq_conflict(void *eloop_ctx
, void *timeout_ctx
)
8489 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
8491 wpa_printf(MSG_DEBUG
, "P2P: Frequency conflict - terminate group");
8492 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_FREQ_CONFLICT
);
8496 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant
*wpa_s
, int freq
,
8497 struct wpa_ssid
*ssid
)
8499 struct wpa_supplicant
*iface
;
8501 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
8502 if (!iface
->current_ssid
||
8503 iface
->current_ssid
->frequency
== freq
||
8504 (iface
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
&&
8505 !iface
->current_ssid
->p2p_group
))
8508 /* Remove the connection with least priority */
8509 if (!wpas_is_p2p_prioritized(iface
)) {
8510 /* STA connection has priority over existing
8511 * P2P connection, so remove the interface. */
8512 wpa_printf(MSG_DEBUG
, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8513 eloop_register_timeout(0, 0,
8514 wpas_p2p_group_freq_conflict
,
8516 /* If connection in progress is P2P connection, do not
8517 * proceed for the connection. */
8523 /* P2P connection has priority, disable the STA network
8525 wpa_supplicant_disable_network(wpa_s
->global
->ifaces
,
8527 wpa_msg(wpa_s
->global
->ifaces
, MSG_INFO
,
8528 WPA_EVENT_FREQ_CONFLICT
" id=%d", ssid
->id
);
8529 os_memset(wpa_s
->global
->ifaces
->pending_bssid
, 0,
8531 /* If P2P connection is in progress, continue
8544 int wpas_p2p_4way_hs_failed(struct wpa_supplicant
*wpa_s
)
8546 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
8548 if (ssid
== NULL
|| !ssid
->p2p_group
)
8551 if (wpa_s
->p2p_last_4way_hs_fail
&&
8552 wpa_s
->p2p_last_4way_hs_fail
== ssid
) {
8553 u8 go_dev_addr
[ETH_ALEN
];
8554 struct wpa_ssid
*persistent
;
8556 if (wpas_p2p_persistent_group(wpa_s
, go_dev_addr
,
8558 ssid
->ssid_len
) <= 0) {
8559 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8563 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8564 MACSTR
, MAC2STR(go_dev_addr
));
8565 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, go_dev_addr
,
8568 if (persistent
== NULL
|| persistent
->mode
!= WPAS_MODE_INFRA
) {
8569 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No matching persistent group stored");
8572 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
8573 P2P_EVENT_PERSISTENT_PSK_FAIL
"%d",
8576 wpa_s
->p2p_last_4way_hs_fail
= NULL
;
8578 * Remove the group from a timeout to avoid issues with caller
8579 * continuing to use the interface if this is on a P2P group
8582 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal
,
8587 wpa_s
->p2p_last_4way_hs_fail
= ssid
;
8592 #ifdef CONFIG_WPS_NFC
8594 static struct wpabuf
* wpas_p2p_nfc_handover(int ndef
, struct wpabuf
*wsc
,
8602 wpa_printf(MSG_DEBUG
, "P2P: No p2p buffer for handover");
8606 wsc_len
= wsc
? wpabuf_len(wsc
) : 0;
8607 ret
= wpabuf_alloc(2 + wsc_len
+ 2 + wpabuf_len(p2p
));
8614 wpabuf_put_be16(ret
, wsc_len
);
8616 wpabuf_put_buf(ret
, wsc
);
8617 wpabuf_put_be16(ret
, wpabuf_len(p2p
));
8618 wpabuf_put_buf(ret
, p2p
);
8622 wpa_hexdump_buf(MSG_DEBUG
,
8623 "P2P: Generated NFC connection handover message", ret
);
8627 tmp
= ndef_build_p2p(ret
);
8630 wpa_printf(MSG_DEBUG
, "P2P: Failed to NDEF encapsulate handover request");
8640 static int wpas_p2p_cli_freq(struct wpa_supplicant
*wpa_s
,
8641 struct wpa_ssid
**ssid
, u8
*go_dev_addr
)
8643 struct wpa_supplicant
*iface
;
8646 os_memset(go_dev_addr
, 0, ETH_ALEN
);
8649 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
8650 if (iface
->wpa_state
< WPA_ASSOCIATING
||
8651 iface
->current_ssid
== NULL
|| iface
->assoc_freq
== 0 ||
8652 !iface
->current_ssid
->p2p_group
||
8653 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
8656 *ssid
= iface
->current_ssid
;
8658 os_memcpy(go_dev_addr
, iface
->go_dev_addr
, ETH_ALEN
);
8659 return iface
->assoc_freq
;
8665 struct wpabuf
* wpas_p2p_nfc_handover_req(struct wpa_supplicant
*wpa_s
,
8668 struct wpabuf
*wsc
, *p2p
;
8669 struct wpa_ssid
*ssid
;
8670 u8 go_dev_addr
[ETH_ALEN
];
8671 int cli_freq
= wpas_p2p_cli_freq(wpa_s
, &ssid
, go_dev_addr
);
8673 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
) {
8674 wpa_printf(MSG_DEBUG
, "P2P: P2P disabled - cannot build handover request");
8678 if (wpa_s
->conf
->wps_nfc_dh_pubkey
== NULL
&&
8679 wps_nfc_gen_dh(&wpa_s
->conf
->wps_nfc_dh_pubkey
,
8680 &wpa_s
->conf
->wps_nfc_dh_privkey
) < 0) {
8681 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No DH key available for handover request");
8685 if (cli_freq
== 0) {
8686 wsc
= wps_build_nfc_handover_req_p2p(
8687 wpa_s
->parent
->wps
, wpa_s
->conf
->wps_nfc_dh_pubkey
);
8690 p2p
= p2p_build_nfc_handover_req(wpa_s
->global
->p2p
, cli_freq
,
8691 go_dev_addr
, ssid
? ssid
->ssid
: NULL
,
8692 ssid
? ssid
->ssid_len
: 0);
8694 return wpas_p2p_nfc_handover(ndef
, wsc
, p2p
);
8698 struct wpabuf
* wpas_p2p_nfc_handover_sel(struct wpa_supplicant
*wpa_s
,
8701 struct wpabuf
*wsc
, *p2p
;
8702 struct wpa_ssid
*ssid
;
8703 u8 go_dev_addr
[ETH_ALEN
];
8704 int cli_freq
= wpas_p2p_cli_freq(wpa_s
, &ssid
, go_dev_addr
);
8706 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
8709 if (!tag
&& wpa_s
->conf
->wps_nfc_dh_pubkey
== NULL
&&
8710 wps_nfc_gen_dh(&wpa_s
->conf
->wps_nfc_dh_pubkey
,
8711 &wpa_s
->conf
->wps_nfc_dh_privkey
) < 0)
8714 if (cli_freq
== 0) {
8715 wsc
= wps_build_nfc_handover_sel_p2p(
8717 tag
? wpa_s
->conf
->wps_nfc_dev_pw_id
:
8718 DEV_PW_NFC_CONNECTION_HANDOVER
,
8719 wpa_s
->conf
->wps_nfc_dh_pubkey
,
8720 tag
? wpa_s
->conf
->wps_nfc_dev_pw
: NULL
);
8723 p2p
= p2p_build_nfc_handover_sel(wpa_s
->global
->p2p
, cli_freq
,
8724 go_dev_addr
, ssid
? ssid
->ssid
: NULL
,
8725 ssid
? ssid
->ssid_len
: 0);
8727 return wpas_p2p_nfc_handover(ndef
, wsc
, p2p
);
8731 static int wpas_p2p_nfc_join_group(struct wpa_supplicant
*wpa_s
,
8732 struct p2p_nfc_params
*params
)
8734 wpa_printf(MSG_DEBUG
, "P2P: Initiate join-group based on NFC "
8735 "connection handover (freq=%d)",
8738 if (params
->go_freq
&& params
->go_ssid_len
) {
8739 wpa_s
->p2p_wps_method
= WPS_NFC
;
8740 wpa_s
->pending_join_wps_method
= WPS_NFC
;
8741 os_memset(wpa_s
->pending_join_iface_addr
, 0, ETH_ALEN
);
8742 os_memcpy(wpa_s
->pending_join_dev_addr
, params
->go_dev_addr
,
8744 return wpas_p2p_join_start(wpa_s
, params
->go_freq
,
8746 params
->go_ssid_len
);
8749 return wpas_p2p_connect(wpa_s
, params
->peer
->p2p_device_addr
, NULL
,
8750 WPS_NFC
, 0, 0, 1, 0, wpa_s
->conf
->p2p_go_intent
,
8751 params
->go_freq
, -1, 0, 1, 1);
8755 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant
*wpa_s
,
8756 struct p2p_nfc_params
*params
, int tag
)
8758 int res
, persistent
;
8759 struct wpa_ssid
*ssid
;
8761 wpa_printf(MSG_DEBUG
, "P2P: Authorize join-group based on NFC "
8762 "connection handover");
8763 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
8764 ssid
= wpa_s
->current_ssid
;
8767 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
8769 if (wpa_s
->ap_iface
== NULL
)
8773 if (wpa_s
== NULL
) {
8774 wpa_printf(MSG_DEBUG
, "P2P: Could not find GO interface");
8778 if (wpa_s
->parent
->p2p_oob_dev_pw_id
!=
8779 DEV_PW_NFC_CONNECTION_HANDOVER
&&
8780 !wpa_s
->parent
->p2p_oob_dev_pw
) {
8781 wpa_printf(MSG_DEBUG
, "P2P: No NFC Dev Pw known");
8784 res
= wpas_ap_wps_add_nfc_pw(
8785 wpa_s
, wpa_s
->parent
->p2p_oob_dev_pw_id
,
8786 wpa_s
->parent
->p2p_oob_dev_pw
,
8787 wpa_s
->parent
->p2p_peer_oob_pk_hash_known
?
8788 wpa_s
->parent
->p2p_peer_oob_pubkey_hash
: NULL
);
8793 wpa_printf(MSG_DEBUG
, "P2P: Negotiated handover - wait for peer to join without invitation");
8797 if (!params
->peer
||
8798 !(params
->peer
->dev_capab
& P2P_DEV_CAPAB_INVITATION_PROCEDURE
))
8801 wpa_printf(MSG_DEBUG
, "P2P: Static handover - invite peer " MACSTR
8802 " to join", MAC2STR(params
->peer
->p2p_device_addr
));
8804 wpa_s
->global
->p2p_invite_group
= wpa_s
;
8805 persistent
= ssid
->p2p_persistent_group
&&
8806 wpas_p2p_get_persistent(wpa_s
->parent
,
8807 params
->peer
->p2p_device_addr
,
8808 ssid
->ssid
, ssid
->ssid_len
);
8809 wpa_s
->parent
->pending_invite_ssid_id
= -1;
8811 return p2p_invite(wpa_s
->global
->p2p
, params
->peer
->p2p_device_addr
,
8812 P2P_INVITE_ROLE_ACTIVE_GO
, wpa_s
->own_addr
,
8813 ssid
->ssid
, ssid
->ssid_len
, ssid
->frequency
,
8814 wpa_s
->global
->p2p_dev_addr
, persistent
, 0,
8815 wpa_s
->parent
->p2p_oob_dev_pw_id
);
8819 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant
*wpa_s
,
8820 struct p2p_nfc_params
*params
,
8823 wpa_printf(MSG_DEBUG
, "P2P: Initiate GO Negotiation based on NFC "
8824 "connection handover");
8825 return wpas_p2p_connect(wpa_s
, params
->peer
->p2p_device_addr
, NULL
,
8826 WPS_NFC
, 0, 0, 0, 0, wpa_s
->conf
->p2p_go_intent
,
8827 forced_freq
, -1, 0, 1, 1);
8831 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant
*wpa_s
,
8832 struct p2p_nfc_params
*params
,
8837 wpa_printf(MSG_DEBUG
, "P2P: Authorize GO Negotiation based on NFC "
8838 "connection handover");
8839 res
= wpas_p2p_connect(wpa_s
, params
->peer
->p2p_device_addr
, NULL
,
8840 WPS_NFC
, 0, 0, 0, 1, wpa_s
->conf
->p2p_go_intent
,
8841 forced_freq
, -1, 0, 1, 1);
8845 res
= wpas_p2p_listen(wpa_s
, 60);
8847 p2p_unauthorize(wpa_s
->global
->p2p
,
8848 params
->peer
->p2p_device_addr
);
8855 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant
*wpa_s
,
8856 const struct wpabuf
*data
,
8857 int sel
, int tag
, int forced_freq
)
8859 const u8
*pos
, *end
;
8861 struct p2p_nfc_params params
;
8864 os_memset(¶ms
, 0, sizeof(params
));
8867 wpa_hexdump_buf(MSG_DEBUG
, "P2P: Received NFC tag payload", data
);
8869 pos
= wpabuf_head(data
);
8870 end
= pos
+ wpabuf_len(data
);
8872 if (end
- pos
< 2) {
8873 wpa_printf(MSG_DEBUG
, "P2P: Not enough data for Length of WSC "
8877 len
= WPA_GET_BE16(pos
);
8879 if (len
> end
- pos
) {
8880 wpa_printf(MSG_DEBUG
, "P2P: Not enough data for WSC "
8884 params
.wsc_attr
= pos
;
8885 params
.wsc_len
= len
;
8888 if (end
- pos
< 2) {
8889 wpa_printf(MSG_DEBUG
, "P2P: Not enough data for Length of P2P "
8893 len
= WPA_GET_BE16(pos
);
8895 if (len
> end
- pos
) {
8896 wpa_printf(MSG_DEBUG
, "P2P: Not enough data for P2P "
8900 params
.p2p_attr
= pos
;
8901 params
.p2p_len
= len
;
8904 wpa_hexdump(MSG_DEBUG
, "P2P: WSC attributes",
8905 params
.wsc_attr
, params
.wsc_len
);
8906 wpa_hexdump(MSG_DEBUG
, "P2P: P2P attributes",
8907 params
.p2p_attr
, params
.p2p_len
);
8909 wpa_hexdump(MSG_DEBUG
,
8910 "P2P: Ignored extra data after P2P attributes",
8914 res
= p2p_process_nfc_connection_handover(wpa_s
->global
->p2p
, ¶ms
);
8918 if (params
.next_step
== NO_ACTION
)
8921 if (params
.next_step
== BOTH_GO
) {
8922 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_NFC_BOTH_GO
"peer=" MACSTR
,
8923 MAC2STR(params
.peer
->p2p_device_addr
));
8927 if (params
.next_step
== PEER_CLIENT
) {
8928 if (!is_zero_ether_addr(params
.go_dev_addr
)) {
8929 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_NFC_PEER_CLIENT
8930 "peer=" MACSTR
" freq=%d go_dev_addr=" MACSTR
8932 MAC2STR(params
.peer
->p2p_device_addr
),
8934 MAC2STR(params
.go_dev_addr
),
8935 wpa_ssid_txt(params
.go_ssid
,
8936 params
.go_ssid_len
));
8938 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_NFC_PEER_CLIENT
8939 "peer=" MACSTR
" freq=%d",
8940 MAC2STR(params
.peer
->p2p_device_addr
),
8946 if (wpas_p2p_cli_freq(wpa_s
, NULL
, NULL
)) {
8947 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_NFC_WHILE_CLIENT
"peer="
8948 MACSTR
, MAC2STR(params
.peer
->p2p_device_addr
));
8952 wpabuf_free(wpa_s
->p2p_oob_dev_pw
);
8953 wpa_s
->p2p_oob_dev_pw
= NULL
;
8955 if (params
.oob_dev_pw_len
< WPS_OOB_PUBKEY_HASH_LEN
+ 2) {
8956 wpa_printf(MSG_DEBUG
, "P2P: No peer OOB Dev Pw "
8961 id
= WPA_GET_BE16(params
.oob_dev_pw
+ WPS_OOB_PUBKEY_HASH_LEN
);
8962 wpa_printf(MSG_DEBUG
, "P2P: Peer OOB Dev Pw %u", id
);
8963 wpa_hexdump(MSG_DEBUG
, "P2P: Peer OOB Public Key hash",
8964 params
.oob_dev_pw
, WPS_OOB_PUBKEY_HASH_LEN
);
8965 os_memcpy(wpa_s
->p2p_peer_oob_pubkey_hash
,
8966 params
.oob_dev_pw
, WPS_OOB_PUBKEY_HASH_LEN
);
8967 wpa_s
->p2p_peer_oob_pk_hash_known
= 1;
8971 wpa_printf(MSG_DEBUG
, "P2P: Static handover - invalid "
8972 "peer OOB Device Password Id %u", id
);
8975 wpa_printf(MSG_DEBUG
, "P2P: Static handover - use peer OOB "
8976 "Device Password Id %u", id
);
8977 wpa_hexdump_key(MSG_DEBUG
, "P2P: Peer OOB Device Password",
8978 params
.oob_dev_pw
+ WPS_OOB_PUBKEY_HASH_LEN
+ 2,
8979 params
.oob_dev_pw_len
-
8980 WPS_OOB_PUBKEY_HASH_LEN
- 2);
8981 wpa_s
->p2p_oob_dev_pw_id
= id
;
8982 wpa_s
->p2p_oob_dev_pw
= wpabuf_alloc_copy(
8983 params
.oob_dev_pw
+ WPS_OOB_PUBKEY_HASH_LEN
+ 2,
8984 params
.oob_dev_pw_len
-
8985 WPS_OOB_PUBKEY_HASH_LEN
- 2);
8986 if (wpa_s
->p2p_oob_dev_pw
== NULL
)
8989 if (wpa_s
->conf
->wps_nfc_dh_pubkey
== NULL
&&
8990 wps_nfc_gen_dh(&wpa_s
->conf
->wps_nfc_dh_pubkey
,
8991 &wpa_s
->conf
->wps_nfc_dh_privkey
) < 0)
8994 wpa_printf(MSG_DEBUG
, "P2P: Using abbreviated WPS handshake "
8995 "without Device Password");
8996 wpa_s
->p2p_oob_dev_pw_id
= DEV_PW_NFC_CONNECTION_HANDOVER
;
8999 switch (params
.next_step
) {
9003 /* already covered above */
9006 return wpas_p2p_nfc_join_group(wpa_s
, ¶ms
);
9008 return wpas_p2p_nfc_auth_join(wpa_s
, ¶ms
, tag
);
9010 return wpas_p2p_nfc_init_go_neg(wpa_s
, ¶ms
, forced_freq
);
9012 /* TODO: use own OOB Dev Pw */
9013 return wpas_p2p_nfc_resp_go_neg(wpa_s
, ¶ms
, forced_freq
);
9020 int wpas_p2p_nfc_tag_process(struct wpa_supplicant
*wpa_s
,
9021 const struct wpabuf
*data
, int forced_freq
)
9023 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
9026 return wpas_p2p_nfc_connection_handover(wpa_s
, data
, 1, 1, forced_freq
);
9030 int wpas_p2p_nfc_report_handover(struct wpa_supplicant
*wpa_s
, int init
,
9031 const struct wpabuf
*req
,
9032 const struct wpabuf
*sel
, int forced_freq
)
9037 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
9040 wpa_printf(MSG_DEBUG
, "NFC: P2P connection handover reported");
9042 wpa_hexdump_ascii(MSG_DEBUG
, "NFC: Req",
9043 wpabuf_head(req
), wpabuf_len(req
));
9044 wpa_hexdump_ascii(MSG_DEBUG
, "NFC: Sel",
9045 wpabuf_head(sel
), wpabuf_len(sel
));
9047 wpa_printf(MSG_DEBUG
, "NFC: Forced freq %d", forced_freq
);
9048 tmp
= ndef_parse_p2p(init
? sel
: req
);
9050 wpa_printf(MSG_DEBUG
, "P2P: Could not parse NDEF");
9054 ret
= wpas_p2p_nfc_connection_handover(wpa_s
, tmp
, init
, 0,
9062 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant
*wpa_s
, int enabled
)
9065 int go_intent
= wpa_s
->conf
->p2p_go_intent
;
9066 struct wpa_supplicant
*iface
;
9068 if (wpa_s
->global
->p2p
== NULL
)
9072 wpa_printf(MSG_DEBUG
, "P2P: Disable use of own NFC Tag");
9073 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
9075 if (!iface
->ap_iface
)
9077 hostapd_wps_nfc_token_disable(iface
->ap_iface
->bss
[0]);
9079 p2p_set_authorized_oob_dev_pw_id(wpa_s
->global
->p2p
, 0,
9081 if (wpa_s
->p2p_nfc_tag_enabled
)
9082 wpas_p2p_remove_pending_group_interface(wpa_s
);
9083 wpa_s
->p2p_nfc_tag_enabled
= 0;
9087 if (wpa_s
->global
->p2p_disabled
)
9090 if (wpa_s
->conf
->wps_nfc_dh_pubkey
== NULL
||
9091 wpa_s
->conf
->wps_nfc_dh_privkey
== NULL
||
9092 wpa_s
->conf
->wps_nfc_dev_pw
== NULL
||
9093 wpa_s
->conf
->wps_nfc_dev_pw_id
< 0x10) {
9094 wpa_printf(MSG_DEBUG
, "P2P: NFC password token not configured "
9095 "to allow static handover cases");
9099 wpa_printf(MSG_DEBUG
, "P2P: Enable use of own NFC Tag");
9101 wpa_s
->p2p_oob_dev_pw_id
= wpa_s
->conf
->wps_nfc_dev_pw_id
;
9102 wpabuf_free(wpa_s
->p2p_oob_dev_pw
);
9103 wpa_s
->p2p_oob_dev_pw
= wpabuf_dup(wpa_s
->conf
->wps_nfc_dev_pw
);
9104 if (wpa_s
->p2p_oob_dev_pw
== NULL
)
9106 wpa_s
->p2p_peer_oob_pk_hash_known
= 0;
9108 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
||
9109 wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
) {
9111 * P2P Group Interface present and the command came on group
9112 * interface, so enable the token for the current interface.
9114 wpa_s
->create_p2p_iface
= 0;
9116 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
9119 if (wpa_s
->create_p2p_iface
) {
9120 enum wpa_driver_if_type iftype
;
9121 /* Prepare to add a new interface for the group */
9122 iftype
= WPA_IF_P2P_GROUP
;
9123 if (go_intent
== 15)
9124 iftype
= WPA_IF_P2P_GO
;
9125 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
9126 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
9127 "interface for the group");
9131 if_addr
= wpa_s
->pending_interface_addr
;
9133 if_addr
= wpa_s
->own_addr
;
9135 wpa_s
->p2p_nfc_tag_enabled
= enabled
;
9137 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
9138 struct hostapd_data
*hapd
;
9139 if (iface
->ap_iface
== NULL
)
9141 hapd
= iface
->ap_iface
->bss
[0];
9142 wpabuf_free(hapd
->conf
->wps_nfc_dh_pubkey
);
9143 hapd
->conf
->wps_nfc_dh_pubkey
=
9144 wpabuf_dup(wpa_s
->conf
->wps_nfc_dh_pubkey
);
9145 wpabuf_free(hapd
->conf
->wps_nfc_dh_privkey
);
9146 hapd
->conf
->wps_nfc_dh_privkey
=
9147 wpabuf_dup(wpa_s
->conf
->wps_nfc_dh_privkey
);
9148 wpabuf_free(hapd
->conf
->wps_nfc_dev_pw
);
9149 hapd
->conf
->wps_nfc_dev_pw
=
9150 wpabuf_dup(wpa_s
->conf
->wps_nfc_dev_pw
);
9151 hapd
->conf
->wps_nfc_dev_pw_id
= wpa_s
->conf
->wps_nfc_dev_pw_id
;
9153 if (hostapd_wps_nfc_token_enable(iface
->ap_iface
->bss
[0]) < 0) {
9154 wpa_dbg(iface
, MSG_DEBUG
,
9155 "P2P: Failed to enable NFC Tag for GO");
9158 p2p_set_authorized_oob_dev_pw_id(
9159 wpa_s
->global
->p2p
, wpa_s
->conf
->wps_nfc_dev_pw_id
, go_intent
,
9165 #endif /* CONFIG_WPS_NFC */
9168 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant
*wpa_s
,
9169 struct wpa_used_freq_data
*freqs
,
9172 u8 curr_chan
, cand
, chan
;
9175 curr_chan
= p2p_get_listen_channel(wpa_s
->global
->p2p
);
9176 for (i
= 0, cand
= 0; i
< num
; i
++) {
9177 ieee80211_freq_to_chan(freqs
[i
].freq
, &chan
);
9178 if (curr_chan
== chan
) {
9183 if (chan
== 1 || chan
== 6 || chan
== 11)
9188 wpa_dbg(wpa_s
, MSG_DEBUG
,
9189 "P2P: Update Listen channel to %u based on operating channel",
9191 p2p_set_listen_channel(wpa_s
->global
->p2p
, 81, cand
, 0);
9196 void wpas_p2p_indicate_state_change(struct wpa_supplicant
*wpa_s
)
9198 struct wpa_used_freq_data
*freqs
;
9199 unsigned int num
= wpa_s
->num_multichan_concurrent
;
9201 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
9205 * If possible, optimize the Listen channel to be a channel that is
9206 * already used by one of the other interfaces.
9208 if (!wpa_s
->conf
->p2p_optimize_listen_chan
)
9211 if (!wpa_s
->current_ssid
|| wpa_s
->wpa_state
!= WPA_COMPLETED
)
9214 freqs
= os_calloc(num
, sizeof(struct wpa_used_freq_data
));
9218 num
= get_shared_radio_freqs_data(wpa_s
, freqs
, num
);
9220 wpas_p2p_optimize_listen_channel(wpa_s
, freqs
, num
);
9225 void wpas_p2p_deinit_iface(struct wpa_supplicant
*wpa_s
)
9227 if (wpa_s
== wpa_s
->global
->p2p_init_wpa_s
&& wpa_s
->global
->p2p
) {
9228 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Disable P2P since removing "
9229 "the management interface is being removed");
9230 wpas_p2p_deinit_global(wpa_s
->global
);
9235 void wpas_p2p_ap_deinit(struct wpa_supplicant
*wpa_s
)
9237 if (wpa_s
->ap_iface
->bss
)
9238 wpa_s
->ap_iface
->bss
[0]->p2p_group
= NULL
;
9239 wpas_p2p_group_deinit(wpa_s
);