3 * Copyright (c) 2009-2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "common/ieee802_11_common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/wpa_ctrl.h"
22 #include "wps/wps_i.h"
24 #include "ap/hostapd.h"
25 #include "ap/p2p_hostapd.h"
26 #include "wpa_supplicant_i.h"
29 #include "config_ssid.h"
35 #include "wps_supplicant.h"
36 #include "p2p_supplicant.h"
40 * How many times to try to scan to find the GO before giving up on join
43 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
46 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
47 static struct wpa_supplicant
*
48 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
50 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
);
51 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
);
52 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
53 const u8
*dev_addr
, enum p2p_wps_method wps_method
);
54 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
);
55 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
);
56 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
);
57 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
);
60 static void wpas_p2p_scan_res_handler(struct wpa_supplicant
*wpa_s
,
61 struct wpa_scan_results
*scan_res
)
65 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
68 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS)",
71 for (i
= 0; i
< scan_res
->num
; i
++) {
72 struct wpa_scan_res
*bss
= scan_res
->res
[i
];
73 if (p2p_scan_res_handler(wpa_s
->global
->p2p
, bss
->bssid
,
74 bss
->freq
, bss
->level
,
75 (const u8
*) (bss
+ 1),
80 p2p_scan_res_handled(wpa_s
->global
->p2p
);
84 static int wpas_p2p_scan(void *ctx
, enum p2p_scan_type type
, int freq
,
85 unsigned int num_req_dev_types
,
86 const u8
*req_dev_types
)
88 struct wpa_supplicant
*wpa_s
= ctx
;
89 struct wpa_driver_scan_params params
;
91 struct wpabuf
*wps_ie
, *ies
;
92 int social_channels
[] = { 2412, 2437, 2462, 0, 0 };
94 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
97 os_memset(¶ms
, 0, sizeof(params
));
99 /* P2P Wildcard SSID */
100 params
.num_ssids
= 1;
101 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
102 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
104 wpa_s
->wps
->dev
.p2p
= 1;
105 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
107 num_req_dev_types
, req_dev_types
);
111 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
116 wpabuf_put_buf(ies
, wps_ie
);
119 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
121 params
.extra_ies
= wpabuf_head(ies
);
122 params
.extra_ies_len
= wpabuf_len(ies
);
125 case P2P_SCAN_SOCIAL
:
126 params
.freqs
= social_channels
;
130 case P2P_SCAN_SPECIFIC
:
131 social_channels
[0] = freq
;
132 social_channels
[1] = 0;
133 params
.freqs
= social_channels
;
135 case P2P_SCAN_SOCIAL_PLUS_ONE
:
136 social_channels
[3] = freq
;
137 params
.freqs
= social_channels
;
141 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
142 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
143 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
145 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
153 #ifdef CONFIG_CLIENT_MLME
154 static void p2p_rx_action_mlme(void *ctx
, const u8
*buf
, size_t len
, int freq
)
156 struct wpa_supplicant
*wpa_s
= ctx
;
157 const struct ieee80211_mgmt
*mgmt
;
160 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
162 mgmt
= (const struct ieee80211_mgmt
*) buf
;
163 hdr_len
= (const u8
*) &mgmt
->u
.action
.u
.vs_public_action
.action
- buf
;
166 p2p_rx_action(wpa_s
->global
->p2p
, mgmt
->da
, mgmt
->sa
, mgmt
->bssid
,
167 mgmt
->u
.action
.category
,
168 &mgmt
->u
.action
.u
.vs_public_action
.action
,
169 len
- hdr_len
, freq
);
171 #endif /* CONFIG_CLIENT_MLME */
174 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
176 switch (p2p_group_interface
) {
177 case P2P_GROUP_INTERFACE_PENDING
:
178 return WPA_IF_P2P_GROUP
;
179 case P2P_GROUP_INTERFACE_GO
:
180 return WPA_IF_P2P_GO
;
181 case P2P_GROUP_INTERFACE_CLIENT
:
182 return WPA_IF_P2P_CLIENT
;
185 return WPA_IF_P2P_GROUP
;
189 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
191 size_t ssid_len
, int *go
)
195 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
196 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
197 if (s
->disabled
!= 0 || !s
->p2p_group
||
198 s
->ssid_len
!= ssid_len
||
199 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
201 if (s
->mode
== WPAS_MODE_P2P_GO
&&
202 s
!= wpa_s
->current_ssid
)
205 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
214 static void wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
)
216 struct wpa_ssid
*ssid
;
220 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
222 ssid
= wpa_s
->current_ssid
;
225 * The current SSID was not known, but there may still be a
226 * pending P2P group interface waiting for provisioning.
228 ssid
= wpa_s
->conf
->ssid
;
230 if (ssid
->p2p_group
&&
231 (ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
232 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)))
237 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
239 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
240 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
241 wpa_s
->reassociate
= 0;
242 wpa_s
->disconnected
= 1;
243 wpa_supplicant_deauthenticate(wpa_s
,
244 WLAN_REASON_DEAUTH_LEAVING
);
248 if (wpa_s
->cross_connect_in_use
) {
249 wpa_s
->cross_connect_in_use
= 0;
250 wpa_msg(wpa_s
->parent
, MSG_INFO
,
251 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
252 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
254 switch (wpa_s
->removal_reason
) {
255 case P2P_GROUP_REMOVAL_REQUESTED
:
256 reason
= " reason=REQUESTED";
258 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
259 reason
= " reason=IDLE";
261 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
262 reason
= " reason=UNAVAILABLE";
268 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_REMOVED
"%s %s%s",
269 wpa_s
->ifname
, gtype
, reason
);
272 wpas_notify_p2p_group_removed(wpa_s
, ssid
, gtype
);
274 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
275 struct wpa_global
*global
;
277 enum wpa_driver_if_type type
;
278 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
280 global
= wpa_s
->global
;
281 ifname
= os_strdup(wpa_s
->ifname
);
282 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
283 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
);
284 wpa_s
= global
->ifaces
;
286 wpa_drv_if_remove(wpa_s
, type
, ifname
);
291 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
292 if (ssid
&& (ssid
->p2p_group
||
293 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
294 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
296 if (ssid
== wpa_s
->current_ssid
)
297 wpa_s
->current_ssid
= NULL
;
298 wpas_notify_network_removed(wpa_s
, ssid
);
299 wpa_config_remove_network(wpa_s
->conf
, id
);
300 wpa_supplicant_clear_status(wpa_s
);
302 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
305 wpa_supplicant_ap_deinit(wpa_s
);
309 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
311 const u8
*ssid
, size_t ssid_len
)
319 if (wpa_s
->go_params
)
320 bssid
= wpa_s
->go_params
->peer_interface_addr
;
322 bssid
= wpa_s
->bssid
;
324 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
326 u8 iface_addr
[ETH_ALEN
];
327 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
329 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
332 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
333 "group is persistent - BSS " MACSTR
" not found",
338 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
340 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
341 "group is persistent - BSS " MACSTR
342 " did not include P2P IE", MAC2STR(bssid
));
343 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
344 (u8
*) (bss
+ 1), bss
->ie_len
);
345 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
346 ((u8
*) bss
+ 1) + bss
->ie_len
,
351 group_capab
= p2p_get_group_capab(p2p
);
352 addr
= p2p_get_go_dev_addr(p2p
);
353 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
354 "group_capab=0x%x", group_capab
);
356 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
357 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
360 os_memset(go_dev_addr
, 0, ETH_ALEN
);
363 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
364 "go_dev_addr=" MACSTR
,
365 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
367 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
371 static void wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
372 struct wpa_ssid
*ssid
,
373 const u8
*go_dev_addr
)
378 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
379 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
380 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
381 if (s
->disabled
== 2 &&
382 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
383 s
->ssid_len
== ssid
->ssid_len
&&
384 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
389 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
391 if (ssid
->passphrase
&& !s
->passphrase
)
393 else if (ssid
->passphrase
&& s
->passphrase
&&
394 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
397 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
400 s
= wpa_config_add_network(wpa_s
->conf
);
403 wpa_config_set_network_defaults(s
);
407 s
->p2p_persistent_group
= 1;
410 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
411 s
->mode
= ssid
->mode
;
412 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
413 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
414 s
->proto
= WPA_PROTO_RSN
;
415 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
417 if (ssid
->passphrase
) {
418 os_free(s
->passphrase
);
419 s
->passphrase
= os_strdup(ssid
->passphrase
);
423 os_memcpy(s
->psk
, ssid
->psk
, 32);
425 if (s
->passphrase
&& !s
->psk_set
)
426 wpa_config_update_psk(s
);
427 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
429 s
->ssid
= os_malloc(ssid
->ssid_len
);
432 s
->ssid_len
= ssid
->ssid_len
;
433 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
436 #ifndef CONFIG_NO_CONFIG_WRITE
437 if (changed
&& wpa_s
->conf
->update_config
&&
438 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
439 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
441 #endif /* CONFIG_NO_CONFIG_WRITE */
445 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
448 struct wpa_ssid
*ssid
;
449 const char *ssid_txt
;
452 u8 go_dev_addr
[ETH_ALEN
];
455 * This callback is likely called for the main interface. Update wpa_s
456 * to use the group interface if a new interface was created for the
459 if (wpa_s
->global
->p2p_group_formation
)
460 wpa_s
= wpa_s
->global
->p2p_group_formation
;
461 wpa_s
->global
->p2p_group_formation
= NULL
;
462 wpa_s
->p2p_in_provisioning
= 0;
465 wpa_msg(wpa_s
->parent
, MSG_INFO
,
466 P2P_EVENT_GROUP_FORMATION_FAILURE
);
467 wpas_p2p_group_delete(wpa_s
);
471 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_FORMATION_SUCCESS
);
473 ssid
= wpa_s
->current_ssid
;
474 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
475 ssid
->mode
= WPAS_MODE_P2P_GO
;
476 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
477 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
482 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
483 client
= ssid
->mode
== WPAS_MODE_INFRA
;
484 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
485 persistent
= ssid
->p2p_persistent_group
;
486 os_memcpy(go_dev_addr
, wpa_s
->parent
->own_addr
,
489 persistent
= wpas_p2p_persistent_group(wpa_s
,
495 client
= wpa_s
->p2p_group_interface
==
496 P2P_GROUP_INTERFACE_CLIENT
;
497 os_memset(go_dev_addr
, 0, ETH_ALEN
);
500 wpa_s
->show_group_started
= 0;
503 * Indicate event only after successfully completed 4-way
504 * handshake, i.e., when the interface is ready for data
507 wpa_s
->show_group_started
= 1;
508 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
510 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
511 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
512 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
514 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
515 MAC2STR(go_dev_addr
),
516 persistent
? " [PERSISTENT]" : "");
517 wpas_p2p_cross_connect_setup(wpa_s
);
518 wpas_p2p_set_group_idle_timeout(wpa_s
);
520 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
521 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
522 "go_dev_addr=" MACSTR
"%s",
523 wpa_s
->ifname
, ssid_txt
, ssid
? ssid
->frequency
: 0,
524 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
525 MAC2STR(go_dev_addr
),
526 persistent
? " [PERSISTENT]" : "");
527 wpas_p2p_cross_connect_setup(wpa_s
);
528 wpas_p2p_set_group_idle_timeout(wpa_s
);
532 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
537 static struct wpa_supplicant
*
538 wpas_get_tx_interface(struct wpa_supplicant
*wpa_s
, const u8
*src
)
540 struct wpa_supplicant
*iface
;
542 if (os_memcmp(src
, wpa_s
->own_addr
, ETH_ALEN
) == 0)
546 * Try to find a group interface that matches with the source address.
548 iface
= wpa_s
->global
->ifaces
;
550 if (os_memcmp(wpa_s
->pending_action_src
,
551 iface
->own_addr
, ETH_ALEN
) == 0)
556 wpa_printf(MSG_DEBUG
, "P2P: Use group interface %s "
557 "instead of interface %s for Action TX",
558 iface
->ifname
, wpa_s
->ifname
);
566 static void wpas_send_action_cb(void *eloop_ctx
, void *timeout_ctx
)
568 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
569 struct wpa_supplicant
*iface
;
573 without_roc
= wpa_s
->pending_action_without_roc
;
574 wpa_s
->pending_action_without_roc
= 0;
575 wpa_printf(MSG_DEBUG
, "P2P: Send Action callback (without_roc=%d "
576 "pending_action_tx=%p)",
577 without_roc
, wpa_s
->pending_action_tx
);
579 if (wpa_s
->pending_action_tx
== NULL
)
583 * This call is likely going to be on the P2P device instance if the
584 * driver uses a separate interface for that purpose. However, some
585 * Action frames are actually sent within a P2P Group and when that is
586 * the case, we need to follow power saving (e.g., GO buffering the
587 * frame for a client in PS mode or a client following the advertised
588 * NoA from its GO). To make that easier for the driver, select the
589 * correct group interface here.
591 iface
= wpas_get_tx_interface(wpa_s
, wpa_s
->pending_action_src
);
593 if (wpa_s
->off_channel_freq
!= wpa_s
->pending_action_freq
&&
594 wpa_s
->pending_action_freq
!= 0 &&
595 wpa_s
->pending_action_freq
!= iface
->assoc_freq
) {
596 wpa_printf(MSG_DEBUG
, "P2P: Pending Action frame TX "
597 "waiting for another freq=%u (off_channel_freq=%u "
599 wpa_s
->pending_action_freq
,
600 wpa_s
->off_channel_freq
,
602 if (without_roc
&& wpa_s
->off_channel_freq
== 0) {
604 * We may get here if wpas_send_action() found us to be
605 * on the correct channel, but remain-on-channel cancel
606 * event was received before getting here.
608 wpa_printf(MSG_DEBUG
, "P2P: Schedule "
609 "remain-on-channel to send Action frame");
610 if (wpa_drv_remain_on_channel(
611 wpa_s
, wpa_s
->pending_action_freq
, 200) <
613 wpa_printf(MSG_DEBUG
, "P2P: Failed to request "
614 "driver to remain on channel (%u "
615 "MHz) for Action Frame TX",
616 wpa_s
->pending_action_freq
);
618 wpa_s
->off_channel_freq
= 0;
619 wpa_s
->roc_waiting_drv_freq
=
620 wpa_s
->pending_action_freq
;
626 wpa_printf(MSG_DEBUG
, "P2P: Sending pending Action frame to "
627 MACSTR
" using interface %s",
628 MAC2STR(wpa_s
->pending_action_dst
), iface
->ifname
);
629 res
= wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
, 0,
630 wpa_s
->pending_action_dst
,
631 wpa_s
->pending_action_src
,
632 wpa_s
->pending_action_bssid
,
633 wpabuf_head(wpa_s
->pending_action_tx
),
634 wpabuf_len(wpa_s
->pending_action_tx
));
636 wpa_printf(MSG_DEBUG
, "P2P: Failed to send the pending "
639 * Use fake TX status event to allow P2P state machine to
642 wpas_send_action_tx_status(
643 wpa_s
, wpa_s
->pending_action_dst
,
644 wpabuf_head(wpa_s
->pending_action_tx
),
645 wpabuf_len(wpa_s
->pending_action_tx
),
646 P2P_SEND_ACTION_FAILED
);
651 void wpas_send_action_tx_status(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
652 const u8
*data
, size_t data_len
,
653 enum p2p_send_action_result result
)
655 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
657 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
660 if (wpa_s
->pending_action_tx
== NULL
) {
661 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - no "
662 "pending operation");
666 if (os_memcmp(dst
, wpa_s
->pending_action_dst
, ETH_ALEN
) != 0) {
667 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - unknown "
668 "destination address");
672 wpabuf_free(wpa_s
->pending_action_tx
);
673 wpa_s
->pending_action_tx
= NULL
;
675 p2p_send_action_cb(wpa_s
->global
->p2p
, wpa_s
->pending_action_freq
,
676 wpa_s
->pending_action_dst
,
677 wpa_s
->pending_action_src
,
678 wpa_s
->pending_action_bssid
,
681 if (wpa_s
->pending_pd_before_join
&&
682 (os_memcmp(wpa_s
->pending_action_dst
, wpa_s
->pending_join_dev_addr
,
684 os_memcmp(wpa_s
->pending_action_dst
,
685 wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
686 wpa_s
->pending_pd_before_join
= 0;
687 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
688 "join-existing-group operation");
689 wpas_p2p_join_start(wpa_s
);
694 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
695 const u8
*src
, const u8
*bssid
, const u8
*buf
,
696 size_t len
, unsigned int wait_time
)
698 struct wpa_supplicant
*wpa_s
= ctx
;
700 wpa_printf(MSG_DEBUG
, "P2P: Send action frame: freq=%d dst=" MACSTR
701 " src=" MACSTR
" bssid=" MACSTR
" len=%d",
702 freq
, MAC2STR(dst
), MAC2STR(src
), MAC2STR(bssid
),
705 if (wpa_s
->pending_action_tx
) {
706 wpa_printf(MSG_DEBUG
, "P2P: Dropped pending Action frame TX "
707 "to " MACSTR
, MAC2STR(wpa_s
->pending_action_dst
));
708 wpabuf_free(wpa_s
->pending_action_tx
);
710 wpa_s
->pending_action_tx
= wpabuf_alloc(len
);
711 if (wpa_s
->pending_action_tx
== NULL
) {
712 wpa_printf(MSG_DEBUG
, "P2P: Failed to allocate Action frame "
713 "TX buffer (len=%llu)", (unsigned long long) len
);
716 wpabuf_put_data(wpa_s
->pending_action_tx
, buf
, len
);
717 os_memcpy(wpa_s
->pending_action_src
, src
, ETH_ALEN
);
718 os_memcpy(wpa_s
->pending_action_dst
, dst
, ETH_ALEN
);
719 os_memcpy(wpa_s
->pending_action_bssid
, bssid
, ETH_ALEN
);
720 wpa_s
->pending_action_freq
= freq
;
722 if (freq
!= 0 && wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_OFFCHANNEL_TX
) {
723 struct wpa_supplicant
*iface
;
725 iface
= wpas_get_tx_interface(wpa_s
, wpa_s
->pending_action_src
);
726 wpa_s
->action_tx_wait_time
= wait_time
;
728 return wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
,
729 wait_time
, wpa_s
->pending_action_dst
,
730 wpa_s
->pending_action_src
,
731 wpa_s
->pending_action_bssid
,
732 wpabuf_head(wpa_s
->pending_action_tx
),
733 wpabuf_len(wpa_s
->pending_action_tx
));
737 struct wpa_supplicant
*tx_iface
;
738 tx_iface
= wpas_get_tx_interface(wpa_s
, src
);
739 if (tx_iface
->assoc_freq
== freq
) {
740 wpa_printf(MSG_DEBUG
, "P2P: Already on requested "
741 "channel (TX interface operating channel)");
746 if (wpa_s
->off_channel_freq
== freq
|| freq
== 0) {
747 wpa_printf(MSG_DEBUG
, "P2P: Already on requested channel; "
748 "send Action frame immediately");
749 /* TODO: Would there ever be need to extend the current
750 * duration on the channel? */
751 wpa_s
->pending_action_without_roc
= 1;
752 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
753 eloop_register_timeout(0, 0, wpas_send_action_cb
, wpa_s
, NULL
);
756 wpa_s
->pending_action_without_roc
= 0;
758 if (wpa_s
->roc_waiting_drv_freq
== freq
) {
759 wpa_printf(MSG_DEBUG
, "P2P: Already waiting for driver to get "
760 "to frequency %u MHz; continue waiting to send the "
761 "Action frame", freq
);
765 wpa_printf(MSG_DEBUG
, "P2P: Schedule Action frame to be transmitted "
766 "once the driver gets to the requested channel");
767 if (wait_time
> wpa_s
->max_remain_on_chan
)
768 wait_time
= wpa_s
->max_remain_on_chan
;
769 if (wpa_drv_remain_on_channel(wpa_s
, freq
, wait_time
) < 0) {
770 wpa_printf(MSG_DEBUG
, "P2P: Failed to request driver "
771 "to remain on channel (%u MHz) for Action "
775 wpa_s
->off_channel_freq
= 0;
776 wpa_s
->roc_waiting_drv_freq
= freq
;
782 static void wpas_send_action_done(void *ctx
)
784 struct wpa_supplicant
*wpa_s
= ctx
;
785 wpa_printf(MSG_DEBUG
, "P2P: Action frame sequence done notification");
786 wpabuf_free(wpa_s
->pending_action_tx
);
787 wpa_s
->pending_action_tx
= NULL
;
788 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_OFFCHANNEL_TX
) {
789 if (wpa_s
->action_tx_wait_time
)
790 wpa_drv_send_action_cancel_wait(wpa_s
);
791 wpa_s
->off_channel_freq
= 0;
792 } else if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
793 wpa_drv_cancel_remain_on_channel(wpa_s
);
794 wpa_s
->off_channel_freq
= 0;
795 wpa_s
->roc_waiting_drv_freq
= 0;
800 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
801 struct p2p_go_neg_results
*params
)
803 if (wpa_s
->go_params
== NULL
) {
804 wpa_s
->go_params
= os_malloc(sizeof(*params
));
805 if (wpa_s
->go_params
== NULL
)
808 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
813 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
814 struct p2p_go_neg_results
*res
)
816 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
817 MAC2STR(res
->peer_interface_addr
));
818 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
819 res
->ssid
, res
->ssid_len
);
820 wpa_supplicant_ap_deinit(wpa_s
);
821 wpas_copy_go_neg_results(wpa_s
, res
);
822 if (res
->wps_method
== WPS_PBC
)
823 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
825 u16 dev_pw_id
= DEV_PW_DEFAULT
;
826 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
827 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
828 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
829 wpa_s
->p2p_pin
, 1, dev_pw_id
);
834 static void p2p_go_configured(void *ctx
, void *data
)
836 struct wpa_supplicant
*wpa_s
= ctx
;
837 struct p2p_go_neg_results
*params
= data
;
838 struct wpa_ssid
*ssid
;
840 ssid
= wpa_s
->current_ssid
;
841 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
842 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
843 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
844 wpa_s
->global
->p2p_group_formation
= NULL
;
845 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
846 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
847 "go_dev_addr=" MACSTR
"%s",
849 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
851 params
->passphrase
? params
->passphrase
: "",
852 MAC2STR(wpa_s
->parent
->own_addr
),
853 params
->persistent_group
? " [PERSISTENT]" : "");
854 if (params
->persistent_group
)
855 wpas_p2p_store_persistent_group(
857 wpa_s
->parent
->own_addr
);
858 wpas_p2p_cross_connect_setup(wpa_s
);
859 wpas_p2p_set_group_idle_timeout(wpa_s
);
863 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
864 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
865 params
->peer_interface_addr
)) {
866 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
870 if (params
->wps_method
== WPS_PBC
)
871 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
873 else if (wpa_s
->p2p_pin
[0])
874 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
875 wpa_s
->p2p_pin
, NULL
, 0);
876 os_free(wpa_s
->go_params
);
877 wpa_s
->go_params
= NULL
;
881 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
882 struct p2p_go_neg_results
*params
,
885 struct wpa_ssid
*ssid
;
887 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0)
890 ssid
= wpa_config_add_network(wpa_s
->conf
);
894 wpas_notify_network_added(wpa_s
, ssid
);
895 wpa_config_set_network_defaults(ssid
);
898 ssid
->p2p_persistent_group
= params
->persistent_group
;
899 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
901 ssid
->frequency
= params
->freq
;
902 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
904 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
905 ssid
->ssid_len
= params
->ssid_len
;
907 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
908 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
909 ssid
->proto
= WPA_PROTO_RSN
;
910 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
911 ssid
->passphrase
= os_strdup(params
->passphrase
);
913 wpa_s
->ap_configured_cb
= p2p_go_configured
;
914 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
915 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
916 wpa_s
->connect_without_scan
= 1;
917 wpa_s
->reassociate
= 1;
918 wpa_s
->disconnected
= 0;
919 wpa_supplicant_req_scan(wpa_s
, 0, 0);
923 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
924 const struct wpa_supplicant
*src
)
926 struct wpa_config
*d
;
927 const struct wpa_config
*s
;
932 #define C(n) if (s->n) d->n = os_strdup(s->n)
941 os_memcpy(d
->device_type
, s
->device_type
, WPS_DEV_TYPE_LEN
);
942 os_memcpy(d
->sec_device_type
, s
->sec_device_type
,
943 sizeof(d
->sec_device_type
));
944 d
->num_sec_device_types
= s
->num_sec_device_types
;
946 d
->p2p_group_idle
= s
->p2p_group_idle
;
947 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
951 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
952 enum wpa_driver_if_type type
)
954 char ifname
[120], force_ifname
[120];
956 if (wpa_s
->pending_interface_name
[0]) {
957 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
958 "- skip creation of a new one");
959 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
960 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
961 "unknown?! ifname='%s'",
962 wpa_s
->pending_interface_name
);
968 os_snprintf(ifname
, sizeof(ifname
), "p2p-%s-%d", wpa_s
->ifname
,
969 wpa_s
->p2p_group_idx
);
970 if (os_strlen(ifname
) >= IFNAMSIZ
&&
971 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
972 /* Try to avoid going over the IFNAMSIZ length limit */
973 os_snprintf(ifname
, sizeof(ifname
), "p2p-%d",
974 wpa_s
->p2p_group_idx
);
976 force_ifname
[0] = '\0';
978 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
980 wpa_s
->p2p_group_idx
++;
982 wpa_s
->pending_interface_type
= type
;
983 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
984 wpa_s
->pending_interface_addr
, NULL
) < 0) {
985 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
990 if (force_ifname
[0]) {
991 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
993 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
994 sizeof(wpa_s
->pending_interface_name
));
996 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
997 sizeof(wpa_s
->pending_interface_name
));
998 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
999 MACSTR
, wpa_s
->pending_interface_name
,
1000 MAC2STR(wpa_s
->pending_interface_addr
));
1006 static void wpas_p2p_remove_pending_group_interface(
1007 struct wpa_supplicant
*wpa_s
)
1009 if (!wpa_s
->pending_interface_name
[0] ||
1010 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
1011 return; /* No pending virtual interface */
1013 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
1014 wpa_s
->pending_interface_name
);
1015 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
1016 wpa_s
->pending_interface_name
);
1017 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1018 wpa_s
->pending_interface_name
[0] = '\0';
1022 static struct wpa_supplicant
*
1023 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1025 struct wpa_interface iface
;
1026 struct wpa_supplicant
*group_wpa_s
;
1028 if (!wpa_s
->pending_interface_name
[0]) {
1029 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1030 if (!wpas_p2p_create_iface(wpa_s
))
1033 * Something has forced us to remove the pending interface; try
1034 * to create a new one and hope for the best that we will get
1035 * the same local address.
1037 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1038 WPA_IF_P2P_CLIENT
) < 0)
1042 os_memset(&iface
, 0, sizeof(iface
));
1043 iface
.ifname
= wpa_s
->pending_interface_name
;
1044 iface
.driver
= wpa_s
->driver
->name
;
1045 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1046 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1047 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1048 if (group_wpa_s
== NULL
) {
1049 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1050 "wpa_supplicant interface");
1053 wpa_s
->pending_interface_name
[0] = '\0';
1054 group_wpa_s
->parent
= wpa_s
;
1055 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1056 P2P_GROUP_INTERFACE_CLIENT
;
1057 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1059 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
1065 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
1068 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1069 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
1070 if (wpa_s
->global
->p2p
)
1071 p2p_group_formation_failed(wpa_s
->global
->p2p
);
1072 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1073 wpa_drv_p2p_group_formation_failed(wpa_s
);
1074 wpas_group_formation_completed(wpa_s
, 0);
1078 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
1080 struct wpa_supplicant
*wpa_s
= ctx
;
1082 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1083 wpa_drv_cancel_remain_on_channel(wpa_s
);
1084 wpa_s
->off_channel_freq
= 0;
1085 wpa_s
->roc_waiting_drv_freq
= 0;
1089 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_FAILURE
"status=%d",
1091 wpas_notify_p2p_go_neg_completed(wpa_s
, res
->status
);
1092 wpas_p2p_remove_pending_group_interface(wpa_s
);
1096 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
);
1097 wpas_notify_p2p_go_neg_completed(wpa_s
, P2P_SC_SUCCESS
);
1099 if (wpa_s
->create_p2p_iface
) {
1100 struct wpa_supplicant
*group_wpa_s
=
1101 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
1102 if (group_wpa_s
== NULL
) {
1103 wpas_p2p_remove_pending_group_interface(wpa_s
);
1106 if (group_wpa_s
!= wpa_s
) {
1107 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
1108 sizeof(group_wpa_s
->p2p_pin
));
1109 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
1111 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1112 wpa_s
->pending_interface_name
[0] = '\0';
1113 group_wpa_s
->p2p_in_provisioning
= 1;
1116 wpas_start_wps_go(group_wpa_s
, res
, 1);
1118 wpas_start_wps_enrollee(group_wpa_s
, res
);
1120 wpa_s
->p2p_in_provisioning
= 1;
1121 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1124 wpas_start_wps_go(wpa_s
, res
, 1);
1126 wpas_start_wps_enrollee(ctx
, res
);
1129 wpa_s
->p2p_long_listen
= 0;
1130 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1132 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1133 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1134 (res
->peer_config_timeout
% 100) * 10000,
1135 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1139 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1141 struct wpa_supplicant
*wpa_s
= ctx
;
1142 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1143 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1145 wpas_notify_p2p_go_neg_req(wpa_s
, src
, dev_passwd_id
);
1149 void wpas_dev_found(void *ctx
, const u8
*addr
,
1150 const struct p2p_peer_info
*info
,
1153 struct wpa_supplicant
*wpa_s
= ctx
;
1154 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1156 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1157 " p2p_dev_addr=" MACSTR
1158 " pri_dev_type=%s name='%s' config_methods=0x%x "
1159 "dev_capab=0x%x group_capab=0x%x",
1160 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
1161 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
1163 info
->device_name
, info
->config_methods
,
1164 info
->dev_capab
, info
->group_capab
);
1166 wpas_notify_p2p_device_found(ctx
, info
->p2p_device_addr
, new_device
);
1170 static void wpas_dev_lost(void *ctx
, const u8
*dev_addr
)
1172 struct wpa_supplicant
*wpa_s
= ctx
;
1174 wpas_notify_p2p_device_lost(wpa_s
, dev_addr
);
1178 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1179 unsigned int duration
,
1180 const struct wpabuf
*probe_resp_ie
)
1182 struct wpa_supplicant
*wpa_s
= ctx
;
1184 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1186 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1187 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1188 "report received Probe Request frames");
1192 wpa_s
->pending_listen_freq
= freq
;
1193 wpa_s
->pending_listen_duration
= duration
;
1195 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1196 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1197 "to remain on channel (%u MHz) for Listen "
1199 wpa_s
->pending_listen_freq
= 0;
1202 wpa_s
->off_channel_freq
= 0;
1203 wpa_s
->roc_waiting_drv_freq
= freq
;
1209 static void wpas_stop_listen(void *ctx
)
1211 struct wpa_supplicant
*wpa_s
= ctx
;
1212 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1213 wpa_drv_cancel_remain_on_channel(wpa_s
);
1214 wpa_s
->off_channel_freq
= 0;
1215 wpa_s
->roc_waiting_drv_freq
= 0;
1217 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
1218 wpa_drv_probe_req_report(wpa_s
, 0);
1222 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1224 struct wpa_supplicant
*wpa_s
= ctx
;
1225 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
));
1229 static struct p2p_srv_bonjour
*
1230 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1231 const struct wpabuf
*query
)
1233 struct p2p_srv_bonjour
*bsrv
;
1236 len
= wpabuf_len(query
);
1237 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1238 struct p2p_srv_bonjour
, list
) {
1239 if (len
== wpabuf_len(bsrv
->query
) &&
1240 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1248 static struct p2p_srv_upnp
*
1249 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1250 const char *service
)
1252 struct p2p_srv_upnp
*usrv
;
1254 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1255 struct p2p_srv_upnp
, list
) {
1256 if (version
== usrv
->version
&&
1257 os_strcmp(service
, usrv
->service
) == 0)
1264 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1269 if (wpabuf_tailroom(resp
) < 5)
1272 /* Length (to be filled) */
1273 len_pos
= wpabuf_put(resp
, 2);
1274 wpabuf_put_u8(resp
, srv_proto
);
1275 wpabuf_put_u8(resp
, srv_trans_id
);
1277 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1278 /* Response Data: empty */
1279 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1283 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1284 struct wpabuf
*resp
, u8 srv_trans_id
)
1286 struct p2p_srv_bonjour
*bsrv
;
1289 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1291 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1292 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1296 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1297 struct p2p_srv_bonjour
, list
) {
1298 if (wpabuf_tailroom(resp
) <
1299 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1301 /* Length (to be filled) */
1302 len_pos
= wpabuf_put(resp
, 2);
1303 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1304 wpabuf_put_u8(resp
, srv_trans_id
);
1306 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1307 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1308 wpabuf_head(bsrv
->resp
),
1309 wpabuf_len(bsrv
->resp
));
1311 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1312 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1313 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1319 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1320 struct wpabuf
*resp
, u8 srv_trans_id
,
1321 const u8
*query
, size_t query_len
)
1323 struct p2p_srv_bonjour
*bsrv
;
1327 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1329 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1330 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1331 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1336 if (query_len
== 0) {
1337 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1341 if (wpabuf_tailroom(resp
) < 5)
1343 /* Length (to be filled) */
1344 len_pos
= wpabuf_put(resp
, 2);
1345 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1346 wpabuf_put_u8(resp
, srv_trans_id
);
1348 wpabuf_set(&buf
, query
, query_len
);
1349 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, &buf
);
1351 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1355 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1356 /* Response Data: empty */
1357 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1363 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1364 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1365 wpabuf_head(bsrv
->resp
), wpabuf_len(bsrv
->resp
));
1367 if (wpabuf_tailroom(resp
) >=
1368 wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
)) {
1370 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1371 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1373 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1377 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1378 struct wpabuf
*resp
, u8 srv_trans_id
)
1380 struct p2p_srv_upnp
*usrv
;
1383 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1385 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1386 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1390 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1391 struct p2p_srv_upnp
, list
) {
1392 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1395 /* Length (to be filled) */
1396 len_pos
= wpabuf_put(resp
, 2);
1397 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1398 wpabuf_put_u8(resp
, srv_trans_id
);
1401 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1403 wpabuf_put_u8(resp
, usrv
->version
);
1404 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1406 wpabuf_put_str(resp
, usrv
->service
);
1407 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1413 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1414 struct wpabuf
*resp
, u8 srv_trans_id
,
1415 const u8
*query
, size_t query_len
)
1417 struct p2p_srv_upnp
*usrv
;
1423 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1426 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1427 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1428 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1433 if (query_len
== 0) {
1434 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1438 if (wpabuf_tailroom(resp
) < 5)
1441 /* Length (to be filled) */
1442 len_pos
= wpabuf_put(resp
, 2);
1443 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1444 wpabuf_put_u8(resp
, srv_trans_id
);
1447 str
= os_malloc(query_len
);
1450 os_memcpy(str
, query
+ 1, query_len
- 1);
1451 str
[query_len
- 1] = '\0';
1453 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1454 struct p2p_srv_upnp
, list
) {
1455 if (version
!= usrv
->version
)
1458 if (os_strcmp(str
, "ssdp:all") != 0 &&
1459 os_strstr(usrv
->service
, str
) == NULL
)
1462 if (wpabuf_tailroom(resp
) < 2)
1466 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1468 wpabuf_put_u8(resp
, version
);
1470 wpabuf_put_u8(resp
, ',');
1474 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1476 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1478 wpabuf_put_str(resp
, usrv
->service
);
1483 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1486 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1487 /* Response Data: empty */
1490 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1494 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
1495 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
1497 struct wpa_supplicant
*wpa_s
= ctx
;
1498 const u8
*pos
= tlvs
;
1499 const u8
*end
= tlvs
+ tlvs_len
;
1502 struct wpabuf
*resp
;
1503 u8 srv_proto
, srv_trans_id
;
1507 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
1509 buf_len
= 2 * tlvs_len
+ 1;
1510 buf
= os_malloc(buf_len
);
1512 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1513 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
1515 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
1520 if (wpa_s
->p2p_sd_over_ctrl_iface
)
1521 return; /* to be processed by an external program */
1523 resp
= wpabuf_alloc(10000);
1527 while (pos
+ 1 < end
) {
1528 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
1529 slen
= WPA_GET_LE16(pos
);
1531 if (pos
+ slen
> end
|| slen
< 2) {
1532 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
1537 tlv_end
= pos
+ slen
;
1540 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1542 srv_trans_id
= *pos
++;
1543 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1546 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
1547 pos
, tlv_end
- pos
);
1550 if (wpa_s
->force_long_sd
) {
1551 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
1553 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1554 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1558 switch (srv_proto
) {
1559 case P2P_SERV_ALL_SERVICES
:
1560 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
1561 "for all services");
1562 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
1563 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1564 wpa_printf(MSG_DEBUG
, "P2P: No service "
1565 "discovery protocols available");
1566 wpas_sd_add_proto_not_avail(
1567 resp
, P2P_SERV_ALL_SERVICES
,
1571 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1572 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1574 case P2P_SERV_BONJOUR
:
1575 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
1576 pos
, tlv_end
- pos
);
1579 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
1580 pos
, tlv_end
- pos
);
1583 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
1584 "protocol %u", srv_proto
);
1585 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
1594 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
1595 update_indic
, tlvs
, tlvs_len
);
1597 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
1603 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
1604 const u8
*tlvs
, size_t tlvs_len
)
1606 struct wpa_supplicant
*wpa_s
= ctx
;
1607 const u8
*pos
= tlvs
;
1608 const u8
*end
= tlvs
+ tlvs_len
;
1614 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
1616 if (tlvs_len
> 1500) {
1617 /* TODO: better way for handling this */
1618 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1619 P2P_EVENT_SERV_DISC_RESP MACSTR
1620 " %u <long response: %u bytes>",
1621 MAC2STR(sa
), update_indic
,
1622 (unsigned int) tlvs_len
);
1624 buf_len
= 2 * tlvs_len
+ 1;
1625 buf
= os_malloc(buf_len
);
1627 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1628 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1629 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
1630 MAC2STR(sa
), update_indic
, buf
);
1636 u8 srv_proto
, srv_trans_id
, status
;
1638 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
1639 slen
= WPA_GET_LE16(pos
);
1641 if (pos
+ slen
> end
|| slen
< 3) {
1642 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
1646 tlv_end
= pos
+ slen
;
1649 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1651 srv_trans_id
= *pos
++;
1652 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1655 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
1658 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
1659 pos
, tlv_end
- pos
);
1664 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
1668 void * wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1669 const struct wpabuf
*tlvs
)
1671 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1672 return (void *) wpa_drv_p2p_sd_request(wpa_s
, dst
, tlvs
);
1673 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1675 return p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
1679 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1680 u8 version
, const char *query
)
1682 struct wpabuf
*tlvs
;
1685 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
1688 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
1689 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
1690 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
1691 wpabuf_put_u8(tlvs
, version
);
1692 wpabuf_put_str(tlvs
, query
);
1693 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
1699 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, void *req
)
1701 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1702 return wpa_drv_p2p_sd_cancel_request(wpa_s
, (u64
) req
);
1703 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1705 return p2p_sd_cancel_request(wpa_s
->global
->p2p
, req
);
1709 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
1710 const u8
*dst
, u8 dialog_token
,
1711 const struct wpabuf
*resp_tlvs
)
1713 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1714 wpa_drv_p2p_sd_response(wpa_s
, freq
, dst
, dialog_token
,
1718 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1720 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
1725 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
1727 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1728 wpa_drv_p2p_service_update(wpa_s
);
1731 if (wpa_s
->global
->p2p
)
1732 p2p_sd_service_update(wpa_s
->global
->p2p
);
1736 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
1738 dl_list_del(&bsrv
->list
);
1739 wpabuf_free(bsrv
->query
);
1740 wpabuf_free(bsrv
->resp
);
1745 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
1747 dl_list_del(&usrv
->list
);
1748 os_free(usrv
->service
);
1753 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
1755 struct p2p_srv_bonjour
*bsrv
, *bn
;
1756 struct p2p_srv_upnp
*usrv
, *un
;
1758 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
1759 struct p2p_srv_bonjour
, list
)
1760 wpas_p2p_srv_bonjour_free(bsrv
);
1762 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
1763 struct p2p_srv_upnp
, list
)
1764 wpas_p2p_srv_upnp_free(usrv
);
1766 wpas_p2p_sd_service_update(wpa_s
);
1770 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
1771 struct wpabuf
*query
, struct wpabuf
*resp
)
1773 struct p2p_srv_bonjour
*bsrv
;
1775 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1778 wpabuf_free(bsrv
->resp
);
1783 bsrv
= os_zalloc(sizeof(*bsrv
));
1786 bsrv
->query
= query
;
1788 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
1790 wpas_p2p_sd_service_update(wpa_s
);
1795 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
1796 const struct wpabuf
*query
)
1798 struct p2p_srv_bonjour
*bsrv
;
1800 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1803 wpas_p2p_srv_bonjour_free(bsrv
);
1804 wpas_p2p_sd_service_update(wpa_s
);
1809 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1810 const char *service
)
1812 struct p2p_srv_upnp
*usrv
;
1814 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
1815 return 0; /* Already listed */
1816 usrv
= os_zalloc(sizeof(*usrv
));
1819 usrv
->version
= version
;
1820 usrv
->service
= os_strdup(service
);
1821 if (usrv
->service
== NULL
) {
1825 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
1827 wpas_p2p_sd_service_update(wpa_s
);
1832 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1833 const char *service
)
1835 struct p2p_srv_upnp
*usrv
;
1837 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
1840 wpas_p2p_srv_upnp_free(usrv
);
1841 wpas_p2p_sd_service_update(wpa_s
);
1846 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
1847 const u8
*peer
, const char *params
,
1848 unsigned int generated_pin
)
1850 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
" %08d%s",
1851 MAC2STR(peer
), generated_pin
, params
);
1855 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
1856 const u8
*peer
, const char *params
)
1858 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
"%s",
1859 MAC2STR(peer
), params
);
1863 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
1864 const u8
*dev_addr
, const u8
*pri_dev_type
,
1865 const char *dev_name
, u16 supp_config_methods
,
1866 u8 dev_capab
, u8 group_capab
)
1868 struct wpa_supplicant
*wpa_s
= ctx
;
1869 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1871 u8 empty_dev_type
[8];
1872 unsigned int generated_pin
= 0;
1874 if (pri_dev_type
== NULL
) {
1875 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
1876 pri_dev_type
= empty_dev_type
;
1878 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
1879 " pri_dev_type=%s name='%s' config_methods=0x%x "
1880 "dev_capab=0x%x group_capab=0x%x",
1882 wps_dev_type_bin2str(pri_dev_type
, devtype
,
1884 dev_name
, supp_config_methods
, dev_capab
, group_capab
);
1885 params
[sizeof(params
) - 1] = '\0';
1887 if (config_methods
& WPS_CONFIG_DISPLAY
) {
1888 generated_pin
= wps_generate_pin();
1889 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
1891 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
1892 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
1893 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1894 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1895 "%s", MAC2STR(peer
), params
);
1899 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
1901 struct wpa_supplicant
*wpa_s
= ctx
;
1902 unsigned int generated_pin
= 0;
1904 if (config_methods
& WPS_CONFIG_DISPLAY
)
1905 wpas_prov_disc_local_keypad(wpa_s
, peer
, "");
1906 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
1907 generated_pin
= wps_generate_pin();
1908 wpas_prov_disc_local_display(wpa_s
, peer
, "", generated_pin
);
1909 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1910 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
,
1913 if (wpa_s
->pending_pd_before_join
&&
1914 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
1915 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
1916 wpa_s
->pending_pd_before_join
= 0;
1917 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
1918 "join-existing-group operation");
1919 wpas_p2p_join_start(wpa_s
);
1924 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
1925 const u8
*go_dev_addr
, const u8
*ssid
,
1926 size_t ssid_len
, int *go
, u8
*group_bssid
,
1927 int *force_freq
, int persistent_group
)
1929 struct wpa_supplicant
*wpa_s
= ctx
;
1931 u8 cur_bssid
[ETH_ALEN
];
1933 struct wpa_supplicant
*grp
;
1935 if (!persistent_group
) {
1936 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1937 " to join an active group", MAC2STR(sa
));
1938 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
1939 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
1941 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
1942 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
1943 "authorized invitation");
1947 * Do not accept the invitation automatically; notify user and
1950 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1953 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
1955 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
1956 "running persistent group");
1958 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
1962 if (!wpa_s
->conf
->persistent_reconnect
)
1963 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1965 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1966 if (s
->disabled
== 2 &&
1967 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
1968 s
->ssid_len
== ssid_len
&&
1969 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
1974 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1975 " requested reinvocation of an unknown group",
1977 return P2P_SC_FAIL_UNKNOWN_GROUP
;
1980 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
1982 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
1983 wpa_printf(MSG_DEBUG
, "P2P: The only available "
1984 "interface is already in use - reject "
1986 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1988 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
1989 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
1991 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
1993 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
1994 "interface address for the group");
1995 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1997 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
2002 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, cur_bssid
) == 0 &&
2003 wpa_s
->assoc_freq
) {
2004 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
2005 "the channel we are already using");
2006 *force_freq
= wpa_s
->assoc_freq
;
2009 res
= wpa_drv_shared_freq(wpa_s
);
2011 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
2012 "with the channel we are already using on a "
2013 "shared interface");
2017 return P2P_SC_SUCCESS
;
2021 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
2022 const u8
*ssid
, size_t ssid_len
,
2023 const u8
*go_dev_addr
, u8 status
,
2026 struct wpa_supplicant
*wpa_s
= ctx
;
2029 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2030 if (s
->disabled
== 2 &&
2031 s
->ssid_len
== ssid_len
&&
2032 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2036 if (status
== P2P_SC_SUCCESS
) {
2037 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2038 " was accepted; op_freq=%d MHz",
2039 MAC2STR(sa
), op_freq
);
2041 wpas_p2p_group_add_persistent(
2042 wpa_s
, s
, s
->mode
== WPAS_MODE_P2P_GO
, 0);
2044 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
2045 wpa_s
->p2p_wps_method
);
2050 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2051 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2052 " was rejected (status %u)", MAC2STR(sa
), status
);
2058 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2059 "sa=" MACSTR
" go_dev_addr=" MACSTR
2060 " bssid=" MACSTR
" unknown-network",
2061 MAC2STR(sa
), MAC2STR(go_dev_addr
),
2064 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2065 "sa=" MACSTR
" go_dev_addr=" MACSTR
2067 MAC2STR(sa
), MAC2STR(go_dev_addr
));
2072 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
"sa=" MACSTR
2073 " persistent=%d", MAC2STR(sa
), s
->id
);
2077 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
)
2079 struct wpa_supplicant
*wpa_s
= ctx
;
2080 struct wpa_ssid
*ssid
;
2083 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2084 "status=%d " MACSTR
,
2085 status
, MAC2STR(bssid
));
2087 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2088 "status=%d ", status
);
2090 wpas_notify_p2p_invitation_result(wpa_s
, status
, bssid
);
2092 if (wpa_s
->pending_invite_ssid_id
== -1)
2093 return; /* Invitation to active group */
2095 if (status
!= P2P_SC_SUCCESS
) {
2096 wpas_p2p_remove_pending_group_interface(wpa_s
);
2100 ssid
= wpa_config_get_network(wpa_s
->conf
,
2101 wpa_s
->pending_invite_ssid_id
);
2103 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
2104 "data matching with invitation");
2108 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
2109 ssid
->mode
== WPAS_MODE_P2P_GO
, 0);
2113 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
2114 struct p2p_channels
*chan
)
2118 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
2121 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2122 chan
->reg_class
[cla
].reg_class
= 81;
2123 chan
->reg_class
[cla
].channels
= 11;
2124 for (i
= 0; i
< 11; i
++)
2125 chan
->reg_class
[cla
].channel
[i
] = i
+ 1;
2128 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
2131 /* Operating class 115 - 5 GHz, channels 36-48 */
2132 chan
->reg_class
[cla
].reg_class
= 115;
2133 chan
->reg_class
[cla
].channels
= 4;
2134 chan
->reg_class
[cla
].channel
[0] = 36;
2135 chan
->reg_class
[cla
].channel
[1] = 40;
2136 chan
->reg_class
[cla
].channel
[2] = 44;
2137 chan
->reg_class
[cla
].channel
[3] = 48;
2140 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
2143 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2144 chan
->reg_class
[cla
].reg_class
= 124;
2145 chan
->reg_class
[cla
].channels
= 4;
2146 chan
->reg_class
[cla
].channel
[0] = 149;
2147 chan
->reg_class
[cla
].channel
[1] = 153;
2148 chan
->reg_class
[cla
].channel
[2] = 157;
2149 chan
->reg_class
[cla
].channel
[3] = 161;
2152 chan
->reg_classes
= cla
;
2157 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
2159 enum hostapd_hw_mode mode
)
2163 for (i
= 0; i
< num_modes
; i
++) {
2164 if (modes
[i
].mode
== mode
)
2172 static int has_channel(struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
2176 for (i
= 0; i
< mode
->num_channels
; i
++) {
2177 if (mode
->channels
[i
].chan
== chan
) {
2179 *flags
= mode
->channels
[i
].flag
;
2180 return !(mode
->channels
[i
].flag
&
2181 (HOSTAPD_CHAN_DISABLED
|
2182 HOSTAPD_CHAN_PASSIVE_SCAN
|
2183 HOSTAPD_CHAN_NO_IBSS
|
2184 HOSTAPD_CHAN_RADAR
));
2192 struct p2p_oper_class_map
{
2193 enum hostapd_hw_mode mode
;
2198 enum { BW20
, BW40PLUS
, BW40MINUS
} bw
;
2201 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
2202 struct p2p_channels
*chan
)
2204 struct hostapd_hw_modes
*modes
, *mode
;
2205 u16 num_modes
, flags
;
2207 struct p2p_oper_class_map op_class
[] = {
2208 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
2209 { HOSTAPD_MODE_IEEE80211G
, 82, 14, 14, 1, BW20
},
2210 #if 0 /* Do not enable HT40 on 2 GHz for now */
2211 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
2212 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
2214 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
2215 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
2216 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
2217 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
2218 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
2219 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
2220 { -1, 0, 0, 0, 0, BW20
}
2223 modes
= wpa_drv_get_hw_feature_data(wpa_s
, &num_modes
, &flags
);
2224 if (modes
== NULL
) {
2225 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
2226 "of all supported channels; assume dualband "
2228 return wpas_p2p_default_channels(wpa_s
, chan
);
2233 for (op
= 0; op_class
[op
].op_class
; op
++) {
2234 struct p2p_oper_class_map
*o
= &op_class
[op
];
2236 struct p2p_reg_class
*reg
= NULL
;
2238 mode
= get_mode(modes
, num_modes
, o
->mode
);
2241 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2243 if (!has_channel(mode
, ch
, &flag
))
2245 if (o
->bw
== BW40MINUS
&&
2246 (!(flag
& HOSTAPD_CHAN_HT40MINUS
) ||
2247 !has_channel(mode
, ch
- 4, NULL
)))
2249 if (o
->bw
== BW40PLUS
&&
2250 (!(flag
& HOSTAPD_CHAN_HT40PLUS
) ||
2251 !has_channel(mode
, ch
+ 4, NULL
)))
2254 wpa_printf(MSG_DEBUG
, "P2P: Add operating "
2255 "class %u", o
->op_class
);
2256 reg
= &chan
->reg_class
[cla
];
2258 reg
->reg_class
= o
->op_class
;
2260 reg
->channel
[reg
->channels
] = ch
;
2264 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
2265 reg
->channel
, reg
->channels
);
2269 chan
->reg_classes
= cla
;
2271 ieee80211_sta_free_hw_features(modes
, num_modes
);
2277 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
2280 struct wpa_supplicant
*wpa_s
= ctx
;
2282 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2283 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
2289 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
2294 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2295 * @global: Pointer to global data from wpa_supplicant_init()
2296 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2297 * Returns: 0 on success, -1 on failure
2299 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
2301 struct p2p_config p2p
;
2305 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
2308 #ifdef CONFIG_CLIENT_MLME
2309 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)) {
2310 wpa_s
->mlme
.public_action_cb
= p2p_rx_action_mlme
;
2311 wpa_s
->mlme
.public_action_cb_ctx
= wpa_s
;
2313 #endif /* CONFIG_CLIENT_MLME */
2315 if (wpa_drv_disable_11b_rates(wpa_s
, 1) < 0) {
2316 wpa_printf(MSG_DEBUG
, "P2P: Failed to disable 11b rates");
2317 /* Continue anyway; this is not really a fatal error */
2323 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2324 struct p2p_params params
;
2326 wpa_printf(MSG_DEBUG
, "P2P: Use driver-based P2P management");
2327 os_memset(¶ms
, 0, sizeof(params
));
2328 params
.dev_name
= wpa_s
->conf
->device_name
;
2329 os_memcpy(params
.pri_dev_type
, wpa_s
->conf
->device_type
,
2331 params
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2332 os_memcpy(params
.sec_dev_type
,
2333 wpa_s
->conf
->sec_device_type
,
2334 params
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2336 if (wpa_drv_p2p_set_params(wpa_s
, ¶ms
) < 0)
2342 os_memset(&p2p
, 0, sizeof(p2p
));
2343 p2p
.msg_ctx
= wpa_s
;
2345 p2p
.p2p_scan
= wpas_p2p_scan
;
2346 p2p
.send_action
= wpas_send_action
;
2347 p2p
.send_action_done
= wpas_send_action_done
;
2348 p2p
.go_neg_completed
= wpas_go_neg_completed
;
2349 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
2350 p2p
.dev_found
= wpas_dev_found
;
2351 p2p
.dev_lost
= wpas_dev_lost
;
2352 p2p
.start_listen
= wpas_start_listen
;
2353 p2p
.stop_listen
= wpas_stop_listen
;
2354 p2p
.send_probe_resp
= wpas_send_probe_resp
;
2355 p2p
.sd_request
= wpas_sd_request
;
2356 p2p
.sd_response
= wpas_sd_response
;
2357 p2p
.prov_disc_req
= wpas_prov_disc_req
;
2358 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
2359 p2p
.invitation_process
= wpas_invitation_process
;
2360 p2p
.invitation_received
= wpas_invitation_received
;
2361 p2p
.invitation_result
= wpas_invitation_result
;
2362 p2p
.get_noa
= wpas_get_noa
;
2364 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2365 os_memcpy(p2p
.dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2366 p2p
.dev_name
= wpa_s
->conf
->device_name
;
2367 p2p
.manufacturer
= wpa_s
->conf
->manufacturer
;
2368 p2p
.model_name
= wpa_s
->conf
->model_name
;
2369 p2p
.model_number
= wpa_s
->conf
->model_number
;
2370 p2p
.serial_number
= wpa_s
->conf
->serial_number
;
2372 os_memcpy(p2p
.uuid
, wpa_s
->wps
->uuid
, 16);
2373 p2p
.config_methods
= wpa_s
->wps
->config_methods
;
2376 if (wpa_s
->conf
->p2p_listen_reg_class
&&
2377 wpa_s
->conf
->p2p_listen_channel
) {
2378 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
2379 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
2383 * Pick one of the social channels randomly as the listen
2386 os_get_random((u8
*) &r
, sizeof(r
));
2387 p2p
.channel
= 1 + (r
% 3) * 5;
2389 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
2391 if (wpa_s
->conf
->p2p_oper_reg_class
&&
2392 wpa_s
->conf
->p2p_oper_channel
) {
2393 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
2394 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
2395 p2p
.cfg_op_channel
= 1;
2396 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
2397 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2400 p2p
.op_reg_class
= 81;
2402 * Use random operation channel from (1, 6, 11) if no other
2403 * preference is indicated.
2405 os_get_random((u8
*) &r
, sizeof(r
));
2406 p2p
.op_channel
= 1 + (r
% 3) * 5;
2407 p2p
.cfg_op_channel
= 0;
2408 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
2409 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2411 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
2412 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
2413 p2p
.country
[2] = 0x04;
2415 os_memcpy(p2p
.country
, "XX\x04", 3);
2417 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
)) {
2418 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
2423 os_memcpy(p2p
.pri_dev_type
, wpa_s
->conf
->device_type
,
2426 p2p
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2427 os_memcpy(p2p
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
2428 p2p
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2430 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
2431 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
2433 p2p
.max_peers
= 100;
2435 if (wpa_s
->conf
->p2p_ssid_postfix
) {
2436 p2p
.ssid_postfix_len
=
2437 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
2438 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
2439 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
2440 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
2441 p2p
.ssid_postfix_len
);
2444 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
2446 global
->p2p
= p2p_init(&p2p
);
2447 if (global
->p2p
== NULL
)
2450 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
2451 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
2453 p2p_add_wps_vendor_extension(
2454 global
->p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
2462 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2463 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2465 * This function deinitialize per-interface P2P data.
2467 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
2469 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
2470 wpa_drv_probe_req_report(wpa_s
, 0);
2471 os_free(wpa_s
->go_params
);
2472 wpa_s
->go_params
= NULL
;
2473 wpabuf_free(wpa_s
->pending_action_tx
);
2474 wpa_s
->pending_action_tx
= NULL
;
2475 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
2476 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2477 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2478 wpa_s
->p2p_long_listen
= 0;
2479 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
2480 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
2481 wpas_p2p_remove_pending_group_interface(wpa_s
);
2483 /* TODO: remove group interface from the driver if this wpa_s instance
2484 * is on top of a P2P group interface */
2489 * wpas_p2p_deinit_global - Deinitialize global P2P module
2490 * @global: Pointer to global data from wpa_supplicant_init()
2492 * This function deinitializes the global (per device) P2P module.
2494 void wpas_p2p_deinit_global(struct wpa_global
*global
)
2496 struct wpa_supplicant
*wpa_s
, *tmp
;
2499 if (global
->p2p
== NULL
)
2502 /* Remove remaining P2P group interfaces */
2503 wpa_s
= global
->ifaces
;
2505 wpas_p2p_service_flush(wpa_s
);
2506 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
2507 wpa_s
= wpa_s
->next
;
2509 enum wpa_driver_if_type type
;
2510 tmp
= global
->ifaces
;
2513 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
2518 ifname
= os_strdup(tmp
->ifname
);
2519 type
= wpas_p2p_if_type(tmp
->p2p_group_interface
);
2520 wpa_supplicant_remove_iface(global
, tmp
);
2522 wpa_drv_if_remove(wpa_s
, type
, ifname
);
2527 * Deinit GO data on any possibly remaining interface (if main
2528 * interface is used as GO).
2530 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2531 if (wpa_s
->ap_iface
)
2532 wpas_p2p_group_deinit(wpa_s
);
2535 p2p_deinit(global
->p2p
);
2540 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
2542 if (wpa_s
->drv_flags
&
2543 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
2544 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
2545 return 1; /* P2P group requires a new interface in every case
2547 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
2548 return 0; /* driver does not support concurrent operations */
2549 if (wpa_s
->global
->ifaces
->next
)
2550 return 1; /* more that one interface already in use */
2551 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
2552 return 1; /* this interface is already in use */
2557 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
2558 const u8
*peer_addr
,
2559 enum p2p_wps_method wps_method
,
2560 int go_intent
, const u8
*own_interface_addr
,
2561 unsigned int force_freq
, int persistent_group
)
2563 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2564 return wpa_drv_p2p_connect(wpa_s
, peer_addr
, wps_method
,
2565 go_intent
, own_interface_addr
,
2566 force_freq
, persistent_group
);
2569 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2570 go_intent
, own_interface_addr
, force_freq
,
2575 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
2576 const u8
*peer_addr
,
2577 enum p2p_wps_method wps_method
,
2578 int go_intent
, const u8
*own_interface_addr
,
2579 unsigned int force_freq
, int persistent_group
)
2581 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2584 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2585 go_intent
, own_interface_addr
, force_freq
,
2590 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
2592 wpa_s
->p2p_join_scan_count
++;
2593 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
2594 wpa_s
->p2p_join_scan_count
);
2595 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
2596 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
2597 " for join operationg - stop join attempt",
2598 MAC2STR(wpa_s
->pending_join_iface_addr
));
2599 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2600 wpa_msg(wpa_s
->parent
, MSG_INFO
,
2601 P2P_EVENT_GROUP_FORMATION_FAILURE
);
2606 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
2607 struct wpa_scan_results
*scan_res
)
2609 struct wpa_bss
*bss
;
2611 u8 iface_addr
[ETH_ALEN
];
2613 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2615 if (wpa_s
->global
->p2p_disabled
)
2618 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for join",
2619 scan_res
? (int) scan_res
->num
: -1);
2622 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
2624 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2625 wpa_s
->pending_join_iface_addr
);
2627 p2p_get_interface_addr(wpa_s
->global
->p2p
,
2628 wpa_s
->pending_join_dev_addr
,
2630 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
2632 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
2633 "address for join from " MACSTR
" to " MACSTR
2634 " based on newly discovered P2P peer entry",
2635 MAC2STR(wpa_s
->pending_join_iface_addr
),
2636 MAC2STR(iface_addr
));
2637 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
2640 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2641 wpa_s
->pending_join_iface_addr
);
2644 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2645 "from P2P peer table: %d MHz", freq
);
2647 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
2650 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2651 "from BSS table: %d MHz", freq
);
2656 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
2657 "prior to joining an existing group (GO " MACSTR
2659 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
2660 wpa_s
->pending_pd_before_join
= 1;
2662 switch (wpa_s
->pending_join_wps_method
) {
2664 case WPS_PIN_DISPLAY
:
2665 method
= WPS_CONFIG_KEYPAD
;
2667 case WPS_PIN_KEYPAD
:
2668 method
= WPS_CONFIG_DISPLAY
;
2671 method
= WPS_CONFIG_PUSHBUTTON
;
2678 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
2679 wpa_s
->pending_join_dev_addr
, method
, 1)
2681 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
2682 "Discovery Request before joining an "
2684 wpa_s
->pending_pd_before_join
= 0;
2689 * Actual join operation will be started from the Action frame
2690 * TX status callback.
2695 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
2696 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2697 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2698 wpas_p2p_check_join_scan_limit(wpa_s
);
2702 /* Start join operation immediately */
2703 wpas_p2p_join_start(wpa_s
);
2707 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
2709 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
2711 struct wpa_driver_scan_params params
;
2712 struct wpabuf
*wps_ie
, *ies
;
2714 os_memset(¶ms
, 0, sizeof(params
));
2716 /* P2P Wildcard SSID */
2717 params
.num_ssids
= 1;
2718 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
2719 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
2721 wpa_s
->wps
->dev
.p2p
= 1;
2722 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
2723 WPS_REQ_ENROLLEE
, 0, NULL
);
2724 if (wps_ie
== NULL
) {
2725 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2729 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
2731 wpabuf_free(wps_ie
);
2732 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2735 wpabuf_put_buf(ies
, wps_ie
);
2736 wpabuf_free(wps_ie
);
2738 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
2740 params
.extra_ies
= wpabuf_head(ies
);
2741 params
.extra_ies_len
= wpabuf_len(ies
);
2744 * Run a scan to update BSS table and start Provision Discovery once
2745 * the new scan results become available.
2747 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
2748 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
2749 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
2751 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
2756 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
2758 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2759 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2760 wpas_p2p_check_join_scan_limit(wpa_s
);
2765 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
2766 const u8
*dev_addr
, enum p2p_wps_method wps_method
)
2768 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
2769 MACSTR
" dev " MACSTR
")",
2770 MAC2STR(iface_addr
), MAC2STR(dev_addr
));
2772 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
2773 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
2774 wpa_s
->pending_join_wps_method
= wps_method
;
2776 /* Make sure we are not running find during connection establishment */
2777 wpas_p2p_stop_find(wpa_s
);
2779 wpa_s
->p2p_join_scan_count
= 0;
2780 wpas_p2p_join_scan(wpa_s
, NULL
);
2785 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
2787 struct wpa_supplicant
*group
;
2788 struct p2p_go_neg_results res
;
2790 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
2793 if (group
!= wpa_s
) {
2794 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
2795 sizeof(group
->p2p_pin
));
2796 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
2799 group
->p2p_in_provisioning
= 1;
2801 os_memset(&res
, 0, sizeof(res
));
2802 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
2804 res
.wps_method
= wpa_s
->pending_join_wps_method
;
2805 wpas_start_wps_enrollee(group
, &res
);
2808 * Allow a longer timeout for join-a-running-group than normal 15
2809 * second group formation timeout since the GO may not have authorized
2810 * our connection yet.
2812 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2813 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
2821 * wpas_p2p_connect - Request P2P Group Formation to be started
2822 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2823 * @peer_addr: Address of the peer P2P Device
2824 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2825 * @persistent_group: Whether to create a persistent group
2826 * @join: Whether to join an existing group (as a client) instead of starting
2827 * Group Owner negotiation; @peer_addr is BSSID in that case
2828 * @auth: Whether to only authorize the connection instead of doing that and
2829 * initiating Group Owner negotiation
2830 * @go_intent: GO Intent or -1 to use default
2831 * @freq: Frequency for the group or 0 for auto-selection
2832 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2833 * failure, -2 on failure due to channel not currently available,
2834 * -3 if forced channel is not supported
2836 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
2837 const char *pin
, enum p2p_wps_method wps_method
,
2838 int persistent_group
, int join
, int auth
, int go_intent
,
2841 int force_freq
= 0, oper_freq
= 0;
2844 enum wpa_driver_if_type iftype
;
2846 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2850 go_intent
= wpa_s
->conf
->p2p_go_intent
;
2853 wpa_s
->p2p_long_listen
= 0;
2855 wpa_s
->p2p_wps_method
= wps_method
;
2858 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
2859 else if (wps_method
== WPS_PIN_DISPLAY
) {
2860 ret
= wps_generate_pin();
2861 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
2863 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
2866 wpa_s
->p2p_pin
[0] = '\0';
2869 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
2871 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
2872 "connect a running group from " MACSTR
,
2873 MAC2STR(peer_addr
));
2874 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
2877 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
2878 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
2880 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
2881 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
2884 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
) <
2890 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
2892 oper_freq
= wpa_s
->assoc_freq
;
2894 oper_freq
= wpa_drv_shared_freq(wpa_s
);
2900 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
2901 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
2902 "(%u MHz) is not supported for P2P uses",
2907 if (oper_freq
> 0 && freq
!= oper_freq
&&
2908 !(wpa_s
->drv_flags
&
2909 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2910 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2911 "on %u MHz while connected on another "
2912 "channel (%u MHz)", freq
, oper_freq
);
2915 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2916 "requested channel (%u MHz)", freq
);
2918 } else if (oper_freq
> 0 &&
2919 !p2p_supported_freq(wpa_s
->global
->p2p
, oper_freq
)) {
2920 if (!(wpa_s
->drv_flags
&
2921 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2922 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2923 "while connected on non-P2P supported "
2924 "channel (%u MHz)", oper_freq
);
2927 wpa_printf(MSG_DEBUG
, "P2P: Current operating channel "
2928 "(%u MHz) not available for P2P - try to use "
2929 "another channel", oper_freq
);
2931 } else if (oper_freq
> 0) {
2932 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2933 "channel we are already using (%u MHz) on another "
2934 "interface", oper_freq
);
2935 force_freq
= oper_freq
;
2938 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
2940 if (!wpa_s
->create_p2p_iface
) {
2942 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2943 go_intent
, wpa_s
->own_addr
,
2944 force_freq
, persistent_group
)
2949 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
2950 go_intent
, wpa_s
->own_addr
,
2951 force_freq
, persistent_group
) < 0)
2956 /* Prepare to add a new interface for the group */
2957 iftype
= WPA_IF_P2P_GROUP
;
2959 iftype
= WPA_IF_P2P_CLIENT
;
2960 else if (go_intent
== 15)
2961 iftype
= WPA_IF_P2P_GO
;
2962 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
2963 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2964 "interface for the group");
2969 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2971 wpa_s
->pending_interface_addr
,
2972 force_freq
, persistent_group
) < 0)
2976 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
, go_intent
,
2977 wpa_s
->pending_interface_addr
,
2978 force_freq
, persistent_group
) < 0) {
2979 wpas_p2p_remove_pending_group_interface(wpa_s
);
2987 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2988 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2989 * @freq: Frequency of the channel in MHz
2990 * @duration: Duration of the stay on the channel in milliseconds
2992 * This callback is called when the driver indicates that it has started the
2993 * requested remain-on-channel duration.
2995 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
2996 unsigned int freq
, unsigned int duration
)
2998 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3000 wpa_s
->roc_waiting_drv_freq
= 0;
3001 wpa_s
->off_channel_freq
= freq
;
3002 wpas_send_action_cb(wpa_s
, NULL
);
3003 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
3004 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
3005 wpa_s
->pending_listen_duration
);
3006 wpa_s
->pending_listen_freq
= 0;
3011 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
3012 unsigned int timeout
)
3014 /* Limit maximum Listen state time based on driver limitation. */
3015 if (timeout
> wpa_s
->max_remain_on_chan
)
3016 timeout
= wpa_s
->max_remain_on_chan
;
3018 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3019 return wpa_drv_p2p_listen(wpa_s
, timeout
);
3021 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
3026 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3027 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3028 * @freq: Frequency of the channel in MHz
3030 * This callback is called when the driver indicates that a remain-on-channel
3031 * operation has been completed, i.e., the duration on the requested channel
3034 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
3037 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
3038 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3039 wpa_s
->p2p_long_listen
, wpa_s
->pending_action_tx
);
3040 wpa_s
->off_channel_freq
= 0;
3041 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3043 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
3044 return; /* P2P module started a new operation */
3045 if (wpa_s
->pending_action_tx
)
3047 if (wpa_s
->p2p_long_listen
> 0)
3048 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
3049 if (wpa_s
->p2p_long_listen
> 0) {
3050 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
3051 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
3057 * wpas_p2p_group_remove - Remove a P2P group
3058 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3059 * @ifname: Network interface name of the group interface or "*" to remove all
3061 * Returns: 0 on success, -1 on failure
3063 * This function is used to remove a P2P group. This can be used to disconnect
3064 * from a group in which the local end is a P2P Client or to end a P2P Group in
3065 * case the local end is the Group Owner. If a virtual network interface was
3066 * created for this group, that interface will be removed. Otherwise, only the
3067 * configured P2P group network will be removed from the interface.
3069 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
3071 struct wpa_global
*global
= wpa_s
->global
;
3073 if (os_strcmp(ifname
, "*") == 0) {
3074 struct wpa_supplicant
*prev
;
3075 wpa_s
= global
->ifaces
;
3078 wpa_s
= wpa_s
->next
;
3079 wpas_p2p_disconnect(prev
);
3084 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3085 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3089 return wpas_p2p_disconnect(wpa_s
);
3093 static void wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
3094 struct p2p_go_neg_results
*params
,
3100 os_memset(params
, 0, sizeof(*params
));
3101 params
->role_go
= 1;
3103 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
3104 "frequency %d MHz", freq
);
3105 params
->freq
= freq
;
3106 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
3107 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
3108 wpa_s
->conf
->p2p_oper_channel
<= 11) {
3109 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3110 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3111 "frequency %d MHz", params
->freq
);
3112 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
3113 wpa_s
->conf
->p2p_oper_reg_class
== 118) {
3114 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3115 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3116 "frequency %d MHz", params
->freq
);
3117 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3118 wpa_s
->best_overall_freq
> 0 &&
3119 p2p_supported_freq(wpa_s
->global
->p2p
,
3120 wpa_s
->best_overall_freq
)) {
3121 params
->freq
= wpa_s
->best_overall_freq
;
3122 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
3123 "channel %d MHz", params
->freq
);
3124 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3125 wpa_s
->best_24_freq
> 0 &&
3126 p2p_supported_freq(wpa_s
->global
->p2p
,
3127 wpa_s
->best_24_freq
)) {
3128 params
->freq
= wpa_s
->best_24_freq
;
3129 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
3130 "channel %d MHz", params
->freq
);
3131 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3132 wpa_s
->best_5_freq
> 0 &&
3133 p2p_supported_freq(wpa_s
->global
->p2p
,
3134 wpa_s
->best_5_freq
)) {
3135 params
->freq
= wpa_s
->best_5_freq
;
3136 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
3137 "channel %d MHz", params
->freq
);
3139 params
->freq
= 2412;
3140 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
3141 "known)", params
->freq
);
3144 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
3145 wpa_s
->assoc_freq
&& !freq
) {
3146 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3148 params
->freq
= wpa_s
->assoc_freq
;
3151 res
= wpa_drv_shared_freq(wpa_s
);
3152 if (res
> 0 && !freq
) {
3153 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3154 "already using on a shared interface");
3160 static struct wpa_supplicant
*
3161 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
3164 struct wpa_supplicant
*group_wpa_s
;
3166 if (!wpas_p2p_create_iface(wpa_s
))
3169 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
3170 WPA_IF_P2P_CLIENT
) < 0)
3172 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
3173 if (group_wpa_s
== NULL
) {
3174 wpas_p2p_remove_pending_group_interface(wpa_s
);
3183 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3184 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3185 * @persistent_group: Whether to create a persistent group
3186 * @freq: Frequency for the group or 0 to indicate no hardcoding
3187 * Returns: 0 on success, -1 on failure
3189 * This function creates a new P2P group with the local end as the Group Owner,
3190 * i.e., without using Group Owner Negotiation.
3192 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
3195 struct p2p_go_neg_results params
;
3198 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3202 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
3204 if (wpa_s
->best_24_freq
> 0 &&
3205 p2p_supported_freq(wpa_s
->global
->p2p
,
3206 wpa_s
->best_24_freq
)) {
3207 freq
= wpa_s
->best_24_freq
;
3208 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
3209 "channel: %d MHz", freq
);
3211 os_get_random((u8
*) &r
, sizeof(r
));
3212 freq
= 2412 + (r
% 3) * 25;
3213 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
3214 "channel: %d MHz", freq
);
3219 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
3221 if (wpa_s
->best_5_freq
> 0 &&
3222 p2p_supported_freq(wpa_s
->global
->p2p
,
3223 wpa_s
->best_5_freq
)) {
3224 freq
= wpa_s
->best_5_freq
;
3225 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
3226 "channel: %d MHz", freq
);
3228 os_get_random((u8
*) &r
, sizeof(r
));
3229 freq
= 5180 + (r
% 4) * 20;
3230 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3231 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
3232 "5 GHz channel for P2P group");
3235 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
3236 "channel: %d MHz", freq
);
3240 if (freq
> 0 && !p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3241 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
3242 "(%u MHz) is not supported for P2P uses",
3247 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
3248 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
3249 params
.persistent_group
= persistent_group
;
3251 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
3254 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3260 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
3261 struct wpa_ssid
*params
, int addr_allocated
)
3263 struct wpa_ssid
*ssid
;
3265 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
3269 wpa_supplicant_ap_deinit(wpa_s
);
3271 ssid
= wpa_config_add_network(wpa_s
->conf
);
3274 wpas_notify_network_added(wpa_s
, ssid
);
3275 wpa_config_set_network_defaults(ssid
);
3276 ssid
->temporary
= 1;
3277 ssid
->proto
= WPA_PROTO_RSN
;
3278 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
3279 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
3280 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
3281 ssid
->ssid
= os_malloc(params
->ssid_len
);
3282 if (ssid
->ssid
== NULL
) {
3283 wpas_notify_network_removed(wpa_s
, ssid
);
3284 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
3287 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
3288 ssid
->ssid_len
= params
->ssid_len
;
3289 ssid
->p2p_group
= 1;
3290 ssid
->export_keys
= 1;
3291 if (params
->psk_set
) {
3292 os_memcpy(ssid
->psk
, params
->psk
, 32);
3295 if (params
->passphrase
)
3296 ssid
->passphrase
= os_strdup(params
->passphrase
);
3298 wpa_supplicant_select_network(wpa_s
, ssid
);
3300 wpa_s
->show_group_started
= 1;
3306 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
3307 struct wpa_ssid
*ssid
, int addr_allocated
,
3310 struct p2p_go_neg_results params
;
3313 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
3316 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
3317 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
3318 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
3323 /* Make sure we are not running find during connection establishment */
3324 wpas_p2p_stop_find(wpa_s
);
3326 if (ssid
->mode
== WPAS_MODE_INFRA
)
3327 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
3329 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
3332 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
3335 if (ssid
->passphrase
== NULL
||
3336 os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
3337 wpa_printf(MSG_DEBUG
, "P2P: Invalid passphrase in persistent "
3341 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
3342 sizeof(params
.passphrase
));
3343 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
3344 params
.ssid_len
= ssid
->ssid_len
;
3345 params
.persistent_group
= 1;
3347 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
3351 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3357 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
3358 struct wpabuf
*proberesp_ies
)
3360 struct wpa_supplicant
*wpa_s
= ctx
;
3361 if (wpa_s
->ap_iface
) {
3362 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
3364 wpabuf_free(hapd
->p2p_beacon_ie
);
3365 hapd
->p2p_beacon_ie
= beacon_ies
;
3367 wpabuf_free(hapd
->p2p_probe_resp_ie
);
3368 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
3370 wpabuf_free(beacon_ies
);
3371 wpabuf_free(proberesp_ies
);
3373 wpa_supplicant_ap_update_beacon(wpa_s
);
3377 static void wpas_p2p_idle_update(void *ctx
, int idle
)
3379 struct wpa_supplicant
*wpa_s
= ctx
;
3380 if (!wpa_s
->ap_iface
)
3382 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
3384 wpas_p2p_set_group_idle_timeout(wpa_s
);
3386 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3390 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
3391 int persistent_group
,
3392 int group_formation
)
3394 struct p2p_group
*group
;
3395 struct p2p_group_config
*cfg
;
3397 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3399 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3402 cfg
= os_zalloc(sizeof(*cfg
));
3406 cfg
->persistent_group
= persistent_group
;
3407 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
3408 if (wpa_s
->max_stations
&&
3409 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
3410 cfg
->max_clients
= wpa_s
->max_stations
;
3412 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
3413 cfg
->cb_ctx
= wpa_s
;
3414 cfg
->ie_update
= wpas_p2p_ie_update
;
3415 cfg
->idle_update
= wpas_p2p_idle_update
;
3417 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
3420 if (!group_formation
)
3421 p2p_group_notif_formation_done(group
);
3422 wpa_s
->p2p_group
= group
;
3427 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3430 if (!wpa_s
->p2p_in_provisioning
) {
3431 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
3432 "provisioning not in progress");
3436 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
3438 if (wpa_s
->global
->p2p
)
3439 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
3440 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3441 wpa_drv_wps_success_cb(wpa_s
, peer_addr
);
3442 wpas_group_formation_completed(wpa_s
, 1);
3446 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3447 const char *config_method
)
3451 if (os_strcmp(config_method
, "display") == 0)
3452 config_methods
= WPS_CONFIG_DISPLAY
;
3453 else if (os_strcmp(config_method
, "keypad") == 0)
3454 config_methods
= WPS_CONFIG_KEYPAD
;
3455 else if (os_strcmp(config_method
, "pbc") == 0 ||
3456 os_strcmp(config_method
, "pushbutton") == 0)
3457 config_methods
= WPS_CONFIG_PUSHBUTTON
;
3461 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3462 return wpa_drv_p2p_prov_disc_req(wpa_s
, peer_addr
,
3466 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
3469 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
3474 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
3477 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
3481 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
3483 if (!wpa_s
->pending_action_tx
)
3486 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
3487 "operation request");
3488 wpabuf_free(wpa_s
->pending_action_tx
);
3489 wpa_s
->pending_action_tx
= NULL
;
3493 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
3494 enum p2p_discovery_type type
,
3495 unsigned int num_req_dev_types
, const u8
*req_dev_types
)
3497 wpas_p2p_clear_pending_action_tx(wpa_s
);
3498 wpa_s
->p2p_long_listen
= 0;
3500 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3501 return wpa_drv_p2p_find(wpa_s
, timeout
, type
);
3503 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3506 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
,
3507 num_req_dev_types
, req_dev_types
);
3511 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
3513 wpas_p2p_clear_pending_action_tx(wpa_s
);
3514 wpa_s
->p2p_long_listen
= 0;
3515 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3516 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3518 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3519 wpa_drv_p2p_stop_find(wpa_s
);
3523 if (wpa_s
->global
->p2p
)
3524 p2p_stop_find(wpa_s
->global
->p2p
);
3526 wpas_p2p_remove_pending_group_interface(wpa_s
);
3530 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
3532 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3533 wpa_s
->p2p_long_listen
= 0;
3537 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
3541 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3544 wpas_p2p_clear_pending_action_tx(wpa_s
);
3548 * This is a request for unlimited Listen state. However, at
3549 * least for now, this is mapped to a Listen state for one
3554 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3555 wpa_s
->p2p_long_listen
= 0;
3557 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
3558 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
3559 wpa_s
->p2p_long_listen
= timeout
* 1000;
3560 eloop_register_timeout(timeout
, 0,
3561 wpas_p2p_long_listen_timeout
,
3569 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
3570 u8
*buf
, size_t len
, int p2p_group
)
3572 struct wpabuf
*p2p_ie
;
3575 if (wpa_s
->global
->p2p_disabled
)
3577 if (wpa_s
->global
->p2p
== NULL
)
3582 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
3583 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
3585 wpabuf_free(p2p_ie
);
3591 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
3592 const u8
*ie
, size_t ie_len
)
3594 if (wpa_s
->global
->p2p_disabled
)
3596 if (wpa_s
->global
->p2p
== NULL
)
3599 return p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, ie
, ie_len
);
3603 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
3604 const u8
*sa
, const u8
*bssid
,
3605 u8 category
, const u8
*data
, size_t len
, int freq
)
3607 if (wpa_s
->global
->p2p_disabled
)
3609 if (wpa_s
->global
->p2p
== NULL
)
3612 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
3617 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
3619 if (wpa_s
->global
->p2p_disabled
)
3621 if (wpa_s
->global
->p2p
== NULL
)
3624 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
3628 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
3630 p2p_group_deinit(wpa_s
->p2p_group
);
3631 wpa_s
->p2p_group
= NULL
;
3635 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
3637 wpa_s
->p2p_long_listen
= 0;
3639 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3640 return wpa_drv_p2p_reject(wpa_s
, addr
);
3642 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3645 return p2p_reject(wpa_s
->global
->p2p
, addr
);
3649 /* Invite to reinvoke a persistent group */
3650 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3651 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
)
3653 enum p2p_invite_role role
;
3656 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3657 role
= P2P_INVITE_ROLE_GO
;
3658 if (peer_addr
== NULL
) {
3659 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
3660 "address in invitation command");
3663 if (wpas_p2p_create_iface(wpa_s
)) {
3664 if (wpas_p2p_add_group_interface(wpa_s
,
3665 WPA_IF_P2P_GO
) < 0) {
3666 wpa_printf(MSG_ERROR
, "P2P: Failed to "
3667 "allocate a new interface for the "
3671 bssid
= wpa_s
->pending_interface_addr
;
3673 bssid
= wpa_s
->own_addr
;
3675 role
= P2P_INVITE_ROLE_CLIENT
;
3676 peer_addr
= ssid
->bssid
;
3678 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
3680 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3681 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3682 ssid
->ssid
, ssid
->ssid_len
,
3685 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3688 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3689 ssid
->ssid
, ssid
->ssid_len
, 0, go_dev_addr
, 1);
3693 /* Invite to join an active group */
3694 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
3695 const u8
*peer_addr
, const u8
*go_dev_addr
)
3697 struct wpa_global
*global
= wpa_s
->global
;
3698 enum p2p_invite_role role
;
3700 struct wpa_ssid
*ssid
;
3702 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3703 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3706 if (wpa_s
== NULL
) {
3707 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
3711 ssid
= wpa_s
->current_ssid
;
3713 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
3718 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3719 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
3720 bssid
= wpa_s
->own_addr
;
3721 if (go_dev_addr
== NULL
)
3722 go_dev_addr
= wpa_s
->parent
->own_addr
;
3724 role
= P2P_INVITE_ROLE_CLIENT
;
3725 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
3726 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
3727 "invite to current group");
3730 bssid
= wpa_s
->bssid
;
3731 if (go_dev_addr
== NULL
&&
3732 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
3733 go_dev_addr
= wpa_s
->go_dev_addr
;
3735 wpa_s
->parent
->pending_invite_ssid_id
= -1;
3737 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3738 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3739 ssid
->ssid
, ssid
->ssid_len
,
3742 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3745 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3746 ssid
->ssid
, ssid
->ssid_len
, wpa_s
->assoc_freq
,
3751 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
3753 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
3754 const char *ssid_txt
;
3755 u8 go_dev_addr
[ETH_ALEN
];
3758 if (!wpa_s
->show_group_started
|| !ssid
)
3761 wpa_s
->show_group_started
= 0;
3763 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
3764 os_memset(go_dev_addr
, 0, ETH_ALEN
);
3765 if (ssid
->bssid_set
)
3766 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
3767 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
3769 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
3771 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
3772 wpa_s
->global
->p2p_group_formation
= NULL
;
3774 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
3776 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
3777 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3778 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3780 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
3781 MAC2STR(go_dev_addr
),
3782 persistent
? " [PERSISTENT]" : "");
3784 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3785 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3786 "go_dev_addr=" MACSTR
"%s",
3787 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
,
3788 ssid
->passphrase
? ssid
->passphrase
: "",
3789 MAC2STR(go_dev_addr
),
3790 persistent
? " [PERSISTENT]" : "");
3794 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
3799 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
3800 u32 interval1
, u32 duration2
, u32 interval2
)
3802 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3804 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3807 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
3808 wpa_s
->current_ssid
== NULL
||
3809 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
3812 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
3813 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
3814 duration1
, interval1
, duration2
, interval2
);
3818 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
3819 unsigned int interval
)
3821 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3824 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3827 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
3831 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
3833 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3835 if (wpa_s
->conf
->p2p_group_idle
== 0) {
3836 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
3841 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
3843 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_IDLE_TIMEOUT
;
3844 wpas_p2p_group_delete(wpa_s
);
3848 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
3850 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3851 if (wpa_s
->conf
->p2p_group_idle
== 0)
3854 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
3857 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
3858 wpa_s
->conf
->p2p_group_idle
);
3859 eloop_register_timeout(wpa_s
->conf
->p2p_group_idle
, 0,
3860 wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3864 void wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3865 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3867 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3869 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3872 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3876 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3877 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3879 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3881 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3884 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3888 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
3890 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
3895 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
3898 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
3899 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
3901 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
)
3902 p2p_set_pri_dev_type(p2p
, wpa_s
->conf
->device_type
);
3905 (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_CONFIG_METHODS
))
3906 p2p_set_config_methods(p2p
, wpa_s
->wps
->config_methods
);
3908 if (wpa_s
->wps
&& (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_UUID
))
3909 p2p_set_uuid(p2p
, wpa_s
->wps
->uuid
);
3911 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_WPS_STRING
) {
3912 p2p_set_manufacturer(p2p
, wpa_s
->conf
->manufacturer
);
3913 p2p_set_model_name(p2p
, wpa_s
->conf
->model_name
);
3914 p2p_set_model_number(p2p
, wpa_s
->conf
->model_number
);
3915 p2p_set_serial_number(p2p
, wpa_s
->conf
->serial_number
);
3918 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
)
3919 p2p_set_sec_dev_types(p2p
,
3920 (void *) wpa_s
->conf
->sec_device_type
,
3921 wpa_s
->conf
->num_sec_device_types
);
3923 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_VENDOR_EXTENSION
) {
3925 p2p_remove_wps_vendor_extensions(p2p
);
3926 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
3927 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
3929 p2p_add_wps_vendor_extension(
3930 p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
3934 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
3935 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
3937 country
[0] = wpa_s
->conf
->country
[0];
3938 country
[1] = wpa_s
->conf
->country
[1];
3940 p2p_set_country(p2p
, country
);
3943 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
3944 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
3945 wpa_s
->conf
->p2p_ssid_postfix
?
3946 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
3950 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
3951 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
3955 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
3958 if (!wpa_s
->ap_iface
)
3960 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
3965 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
3967 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3969 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3972 wpa_s
->global
->cross_connection
= enabled
;
3973 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
3976 struct wpa_supplicant
*iface
;
3978 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
3980 if (iface
->cross_connect_enabled
== 0)
3983 iface
->cross_connect_enabled
= 0;
3984 iface
->cross_connect_in_use
= 0;
3985 wpa_msg(iface
->parent
, MSG_INFO
,
3986 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
3987 iface
->ifname
, iface
->cross_connect_uplink
);
3995 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
3997 struct wpa_supplicant
*iface
;
3999 if (!uplink
->global
->cross_connection
)
4002 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
4003 if (!iface
->cross_connect_enabled
)
4005 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
4008 if (iface
->ap_iface
== NULL
)
4010 if (iface
->cross_connect_in_use
)
4013 iface
->cross_connect_in_use
= 1;
4014 wpa_msg(iface
->parent
, MSG_INFO
,
4015 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
4016 iface
->ifname
, iface
->cross_connect_uplink
);
4021 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
4023 struct wpa_supplicant
*iface
;
4025 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
4026 if (!iface
->cross_connect_enabled
)
4028 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
4031 if (!iface
->cross_connect_in_use
)
4034 wpa_msg(iface
->parent
, MSG_INFO
,
4035 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
4036 iface
->ifname
, iface
->cross_connect_uplink
);
4037 iface
->cross_connect_in_use
= 0;
4042 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
4044 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
4045 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4046 wpa_s
->cross_connect_disallowed
)
4047 wpas_p2p_disable_cross_connect(wpa_s
);
4049 wpas_p2p_enable_cross_connect(wpa_s
);
4050 if (!wpa_s
->ap_iface
)
4051 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
4055 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
4057 wpas_p2p_disable_cross_connect(wpa_s
);
4058 if (!wpa_s
->ap_iface
)
4059 wpas_p2p_set_group_idle_timeout(wpa_s
);
4063 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
4065 struct wpa_supplicant
*iface
;
4067 if (!wpa_s
->global
->cross_connection
)
4070 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
4073 if (iface
->drv_flags
&
4074 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
4076 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
4079 wpa_s
->cross_connect_enabled
= 1;
4080 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
4081 sizeof(wpa_s
->cross_connect_uplink
));
4082 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
4083 "%s to %s whenever uplink is available",
4084 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4086 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
4087 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4088 iface
->cross_connect_disallowed
||
4089 iface
->wpa_state
!= WPA_COMPLETED
)
4092 wpa_s
->cross_connect_in_use
= 1;
4093 wpa_msg(wpa_s
->parent
, MSG_INFO
,
4094 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
4095 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4101 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
4103 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
4104 !wpa_s
->p2p_in_provisioning
)
4105 return 0; /* not P2P client operation */
4107 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
4109 if (wpa_s
!= wpa_s
->parent
)
4110 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
4112 if (wpa_s
->global
->p2p
)
4113 p2p_group_formation_failed(wpa_s
->global
->p2p
);
4115 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4116 wpa_s
->parent
, NULL
);
4118 wpas_group_formation_completed(wpa_s
, 0);
4123 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
4125 struct p2p_channels chan
;
4127 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
4130 os_memset(&chan
, 0, sizeof(chan
));
4131 if (wpas_p2p_setup_channels(wpa_s
, &chan
)) {
4132 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
4137 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
);
4141 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
4143 struct wpa_global
*global
= wpa_s
->global
;
4147 if (global
->p2p
== NULL
)
4150 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
4152 if (wpa_s
->pending_interface_name
[0] &&
4153 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
4156 peer
= p2p_get_go_neg_peer(global
->p2p
);
4158 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
4159 MACSTR
, MAC2STR(peer
));
4160 p2p_unauthorize(global
->p2p
, peer
);
4163 wpas_p2p_stop_find(wpa_s
);
4165 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4166 if (wpa_s
== global
->p2p_group_formation
&&
4167 (wpa_s
->p2p_in_provisioning
||
4168 wpa_s
->parent
->pending_interface_type
==
4169 WPA_IF_P2P_CLIENT
)) {
4170 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
4171 "formation found - cancelling",
4174 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4175 wpa_s
->parent
, NULL
);
4176 wpas_p2p_group_delete(wpa_s
);
4182 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
4190 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
4192 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
4195 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
4196 "being available anymore");
4197 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_UNAVAILABLE
;
4198 wpas_p2p_group_delete(wpa_s
);
4202 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
4203 int freq_24
, int freq_5
, int freq_overall
)
4205 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4206 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4208 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
4212 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
4215 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4217 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4220 if (hwaddr_aton(addr
, peer
))
4223 return p2p_unauthorize(p2p
, peer
);
4228 * wpas_p2p_disconnect - Disconnect from a P2P Group
4229 * @wpa_s: Pointer to wpa_supplicant data
4230 * Returns: 0 on success, -1 on failure
4232 * This can be used to disconnect from a group in which the local end is a P2P
4233 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4234 * virtual network interface was created for this group, that interface will be
4235 * removed. Otherwise, only the configured P2P group network will be removed
4236 * from the interface.
4238 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
4244 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_REQUESTED
;
4245 wpas_p2p_group_delete(wpa_s
);