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
)
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
)
86 struct wpa_supplicant
*wpa_s
= ctx
;
87 struct wpa_driver_scan_params params
;
89 struct wpabuf
*wps_ie
, *ies
;
90 int social_channels
[] = { 2412, 2437, 2462, 0, 0 };
92 os_memset(¶ms
, 0, sizeof(params
));
94 /* P2P Wildcard SSID */
96 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
97 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
99 wpa_s
->wps
->dev
.p2p
= 1;
100 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
105 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
110 wpabuf_put_buf(ies
, wps_ie
);
113 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
115 params
.extra_ies
= wpabuf_head(ies
);
116 params
.extra_ies_len
= wpabuf_len(ies
);
119 case P2P_SCAN_SOCIAL
:
120 params
.freqs
= social_channels
;
124 case P2P_SCAN_SPECIFIC
:
125 social_channels
[0] = freq
;
126 social_channels
[1] = 0;
127 params
.freqs
= social_channels
;
129 case P2P_SCAN_SOCIAL_PLUS_ONE
:
130 social_channels
[3] = freq
;
131 params
.freqs
= social_channels
;
135 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
136 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
137 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
139 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
147 #ifdef CONFIG_CLIENT_MLME
148 static void p2p_rx_action_mlme(void *ctx
, const u8
*buf
, size_t len
, int freq
)
150 struct wpa_supplicant
*wpa_s
= ctx
;
151 const struct ieee80211_mgmt
*mgmt
;
154 if (wpa_s
->global
->p2p_disabled
)
156 mgmt
= (const struct ieee80211_mgmt
*) buf
;
157 hdr_len
= (const u8
*) &mgmt
->u
.action
.u
.vs_public_action
.action
- buf
;
160 p2p_rx_action(wpa_s
->global
->p2p
, mgmt
->da
, mgmt
->sa
, mgmt
->bssid
,
161 mgmt
->u
.action
.category
,
162 &mgmt
->u
.action
.u
.vs_public_action
.action
,
163 len
- hdr_len
, freq
);
165 #endif /* CONFIG_CLIENT_MLME */
168 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
170 switch (p2p_group_interface
) {
171 case P2P_GROUP_INTERFACE_PENDING
:
172 return WPA_IF_P2P_GROUP
;
173 case P2P_GROUP_INTERFACE_GO
:
174 return WPA_IF_P2P_GO
;
175 case P2P_GROUP_INTERFACE_CLIENT
:
176 return WPA_IF_P2P_CLIENT
;
179 return WPA_IF_P2P_GROUP
;
183 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
185 size_t ssid_len
, int *go
)
189 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
190 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
191 if (s
->disabled
!= 0 || !s
->p2p_group
||
192 s
->ssid_len
!= ssid_len
||
193 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
195 if (s
->mode
== WPAS_MODE_P2P_GO
&&
196 s
!= wpa_s
->current_ssid
)
199 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
208 static void wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
)
210 struct wpa_ssid
*ssid
;
214 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
216 ssid
= wpa_s
->current_ssid
;
219 * The current SSID was not known, but there may still be a
220 * pending P2P group interface waiting for provisioning.
222 ssid
= wpa_s
->conf
->ssid
;
224 if (ssid
->p2p_group
&&
225 (ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
226 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)))
231 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
233 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
234 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
235 wpa_s
->reassociate
= 0;
236 wpa_s
->disconnected
= 1;
237 wpa_supplicant_deauthenticate(wpa_s
,
238 WLAN_REASON_DEAUTH_LEAVING
);
242 if (wpa_s
->cross_connect_in_use
) {
243 wpa_s
->cross_connect_in_use
= 0;
244 wpa_msg(wpa_s
->parent
, MSG_INFO
,
245 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
246 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
248 switch (wpa_s
->removal_reason
) {
249 case P2P_GROUP_REMOVAL_REQUESTED
:
250 reason
= " reason=REQUESTED";
252 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
253 reason
= " reason=IDLE";
255 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
256 reason
= " reason=UNAVAILABLE";
262 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_REMOVED
"%s %s%s",
263 wpa_s
->ifname
, gtype
, reason
);
264 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
265 struct wpa_global
*global
;
267 enum wpa_driver_if_type type
;
268 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
270 global
= wpa_s
->global
;
271 ifname
= os_strdup(wpa_s
->ifname
);
272 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
273 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
);
274 wpa_s
= global
->ifaces
;
276 wpa_drv_if_remove(wpa_s
, type
, ifname
);
281 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
282 if (ssid
&& (ssid
->p2p_group
||
283 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
284 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
286 if (ssid
== wpa_s
->current_ssid
)
287 wpa_s
->current_ssid
= NULL
;
288 wpas_notify_network_removed(wpa_s
, ssid
);
289 wpa_config_remove_network(wpa_s
->conf
, id
);
290 wpa_supplicant_clear_status(wpa_s
);
292 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
295 wpa_supplicant_ap_deinit(wpa_s
);
299 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
301 const u8
*ssid
, size_t ssid_len
)
309 if (wpa_s
->go_params
)
310 bssid
= wpa_s
->go_params
->peer_interface_addr
;
312 bssid
= wpa_s
->bssid
;
314 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
316 u8 iface_addr
[ETH_ALEN
];
317 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
319 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
322 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
323 "group is persistent - BSS " MACSTR
" not found",
328 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
330 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
331 "group is persistent - BSS " MACSTR
332 " did not include P2P IE", MAC2STR(bssid
));
333 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
334 (u8
*) (bss
+ 1), bss
->ie_len
);
335 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
336 ((u8
*) bss
+ 1) + bss
->ie_len
,
341 group_capab
= p2p_get_group_capab(p2p
);
342 addr
= p2p_get_go_dev_addr(p2p
);
343 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
344 "group_capab=0x%x", group_capab
);
346 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
347 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
350 os_memset(go_dev_addr
, 0, ETH_ALEN
);
353 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
354 "go_dev_addr=" MACSTR
,
355 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
357 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
361 static void wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
362 struct wpa_ssid
*ssid
,
363 const u8
*go_dev_addr
)
368 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
369 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
370 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
371 if (s
->disabled
== 2 &&
372 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
373 s
->ssid_len
== ssid
->ssid_len
&&
374 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
379 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
381 if (ssid
->passphrase
&& !s
->passphrase
)
383 else if (ssid
->passphrase
&& s
->passphrase
&&
384 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
387 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
390 s
= wpa_config_add_network(wpa_s
->conf
);
393 wpa_config_set_network_defaults(s
);
397 s
->p2p_persistent_group
= 1;
400 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
401 s
->mode
= ssid
->mode
;
402 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
403 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
404 s
->proto
= WPA_PROTO_RSN
;
405 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
406 if (ssid
->passphrase
) {
407 os_free(s
->passphrase
);
408 s
->passphrase
= os_strdup(ssid
->passphrase
);
412 os_memcpy(s
->psk
, ssid
->psk
, 32);
414 if (s
->passphrase
&& !s
->psk_set
)
415 wpa_config_update_psk(s
);
416 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
418 s
->ssid
= os_malloc(ssid
->ssid_len
);
421 s
->ssid_len
= ssid
->ssid_len
;
422 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
425 #ifndef CONFIG_NO_CONFIG_WRITE
426 if (changed
&& wpa_s
->conf
->update_config
&&
427 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
428 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
430 #endif /* CONFIG_NO_CONFIG_WRITE */
434 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
437 struct wpa_ssid
*ssid
;
438 const char *ssid_txt
;
441 u8 go_dev_addr
[ETH_ALEN
];
444 * This callback is likely called for the main interface. Update wpa_s
445 * to use the group interface if a new interface was created for the
448 if (wpa_s
->global
->p2p_group_formation
)
449 wpa_s
= wpa_s
->global
->p2p_group_formation
;
450 wpa_s
->global
->p2p_group_formation
= NULL
;
451 wpa_s
->p2p_in_provisioning
= 0;
454 wpa_msg(wpa_s
->parent
, MSG_INFO
,
455 P2P_EVENT_GROUP_FORMATION_FAILURE
);
456 wpas_p2p_group_delete(wpa_s
);
460 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_FORMATION_SUCCESS
);
462 ssid
= wpa_s
->current_ssid
;
463 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
464 ssid
->mode
= WPAS_MODE_P2P_GO
;
465 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
466 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
471 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
472 client
= ssid
->mode
== WPAS_MODE_INFRA
;
473 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
474 persistent
= ssid
->p2p_persistent_group
;
475 os_memcpy(go_dev_addr
, wpa_s
->parent
->own_addr
,
478 persistent
= wpas_p2p_persistent_group(wpa_s
,
484 client
= wpa_s
->p2p_group_interface
==
485 P2P_GROUP_INTERFACE_CLIENT
;
488 wpa_s
->show_group_started
= 0;
491 * Indicate event only after successfully completed 4-way
492 * handshake, i.e., when the interface is ready for data
495 wpa_s
->show_group_started
= 1;
496 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
498 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
499 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
500 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
502 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
503 MAC2STR(go_dev_addr
),
504 persistent
? " [PERSISTENT]" : "");
505 wpas_p2p_cross_connect_setup(wpa_s
);
506 wpas_p2p_set_group_idle_timeout(wpa_s
);
508 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
509 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
510 "go_dev_addr=" MACSTR
"%s",
511 wpa_s
->ifname
, ssid_txt
, ssid
? ssid
->frequency
: 0,
512 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
513 MAC2STR(go_dev_addr
),
514 persistent
? " [PERSISTENT]" : "");
515 wpas_p2p_cross_connect_setup(wpa_s
);
516 wpas_p2p_set_group_idle_timeout(wpa_s
);
520 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
525 static struct wpa_supplicant
*
526 wpas_get_tx_interface(struct wpa_supplicant
*wpa_s
, const u8
*src
)
528 struct wpa_supplicant
*iface
;
530 if (os_memcmp(src
, wpa_s
->own_addr
, ETH_ALEN
) == 0)
534 * Try to find a group interface that matches with the source address.
536 iface
= wpa_s
->global
->ifaces
;
538 if (os_memcmp(wpa_s
->pending_action_src
,
539 iface
->own_addr
, ETH_ALEN
) == 0)
544 wpa_printf(MSG_DEBUG
, "P2P: Use group interface %s "
545 "instead of interface %s for Action TX",
546 iface
->ifname
, wpa_s
->ifname
);
554 static void wpas_send_action_cb(void *eloop_ctx
, void *timeout_ctx
)
556 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
557 struct wpa_supplicant
*iface
;
561 without_roc
= wpa_s
->pending_action_without_roc
;
562 wpa_s
->pending_action_without_roc
= 0;
563 wpa_printf(MSG_DEBUG
, "P2P: Send Action callback (without_roc=%d "
564 "pending_action_tx=%p)",
565 without_roc
, wpa_s
->pending_action_tx
);
567 if (wpa_s
->pending_action_tx
== NULL
)
571 * This call is likely going to be on the P2P device instance if the
572 * driver uses a separate interface for that purpose. However, some
573 * Action frames are actually sent within a P2P Group and when that is
574 * the case, we need to follow power saving (e.g., GO buffering the
575 * frame for a client in PS mode or a client following the advertised
576 * NoA from its GO). To make that easier for the driver, select the
577 * correct group interface here.
579 iface
= wpas_get_tx_interface(wpa_s
, wpa_s
->pending_action_src
);
581 if (wpa_s
->off_channel_freq
!= wpa_s
->pending_action_freq
&&
582 wpa_s
->pending_action_freq
!= 0 &&
583 wpa_s
->pending_action_freq
!= iface
->assoc_freq
) {
584 wpa_printf(MSG_DEBUG
, "P2P: Pending Action frame TX "
585 "waiting for another freq=%u (off_channel_freq=%u "
587 wpa_s
->pending_action_freq
,
588 wpa_s
->off_channel_freq
,
590 if (without_roc
&& wpa_s
->off_channel_freq
== 0) {
592 * We may get here if wpas_send_action() found us to be
593 * on the correct channel, but remain-on-channel cancel
594 * event was received before getting here.
596 wpa_printf(MSG_DEBUG
, "P2P: Schedule "
597 "remain-on-channel to send Action frame");
598 if (wpa_drv_remain_on_channel(
599 wpa_s
, wpa_s
->pending_action_freq
, 200) <
601 wpa_printf(MSG_DEBUG
, "P2P: Failed to request "
602 "driver to remain on channel (%u "
603 "MHz) for Action Frame TX",
604 wpa_s
->pending_action_freq
);
606 wpa_s
->off_channel_freq
= 0;
607 wpa_s
->roc_waiting_drv_freq
=
608 wpa_s
->pending_action_freq
;
614 wpa_printf(MSG_DEBUG
, "P2P: Sending pending Action frame to "
615 MACSTR
" using interface %s",
616 MAC2STR(wpa_s
->pending_action_dst
), iface
->ifname
);
617 res
= wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
, 0,
618 wpa_s
->pending_action_dst
,
619 wpa_s
->pending_action_src
,
620 wpa_s
->pending_action_bssid
,
621 wpabuf_head(wpa_s
->pending_action_tx
),
622 wpabuf_len(wpa_s
->pending_action_tx
));
624 wpa_printf(MSG_DEBUG
, "P2P: Failed to send the pending "
627 * Use fake TX status event to allow P2P state machine to
630 wpas_send_action_tx_status(
631 wpa_s
, wpa_s
->pending_action_dst
,
632 wpabuf_head(wpa_s
->pending_action_tx
),
633 wpabuf_len(wpa_s
->pending_action_tx
),
634 P2P_SEND_ACTION_FAILED
);
639 void wpas_send_action_tx_status(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
640 const u8
*data
, size_t data_len
,
641 enum p2p_send_action_result result
)
643 if (wpa_s
->global
->p2p_disabled
)
645 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
648 if (wpa_s
->pending_action_tx
== NULL
) {
649 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - no "
650 "pending operation");
654 if (os_memcmp(dst
, wpa_s
->pending_action_dst
, ETH_ALEN
) != 0) {
655 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - unknown "
656 "destination address");
660 wpabuf_free(wpa_s
->pending_action_tx
);
661 wpa_s
->pending_action_tx
= NULL
;
663 p2p_send_action_cb(wpa_s
->global
->p2p
, wpa_s
->pending_action_freq
,
664 wpa_s
->pending_action_dst
,
665 wpa_s
->pending_action_src
,
666 wpa_s
->pending_action_bssid
,
669 if (wpa_s
->pending_pd_before_join
&&
670 (os_memcmp(wpa_s
->pending_action_dst
, wpa_s
->pending_join_dev_addr
,
672 os_memcmp(wpa_s
->pending_action_dst
,
673 wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
674 wpa_s
->pending_pd_before_join
= 0;
675 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
676 "join-existing-group operation");
677 wpas_p2p_join_start(wpa_s
);
682 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
683 const u8
*src
, const u8
*bssid
, const u8
*buf
,
684 size_t len
, unsigned int wait_time
)
686 struct wpa_supplicant
*wpa_s
= ctx
;
688 wpa_printf(MSG_DEBUG
, "P2P: Send action frame: freq=%d dst=" MACSTR
689 " src=" MACSTR
" bssid=" MACSTR
" len=%d",
690 freq
, MAC2STR(dst
), MAC2STR(src
), MAC2STR(bssid
),
693 if (wpa_s
->pending_action_tx
) {
694 wpa_printf(MSG_DEBUG
, "P2P: Dropped pending Action frame TX "
695 "to " MACSTR
, MAC2STR(wpa_s
->pending_action_dst
));
696 wpabuf_free(wpa_s
->pending_action_tx
);
698 wpa_s
->pending_action_tx
= wpabuf_alloc(len
);
699 if (wpa_s
->pending_action_tx
== NULL
) {
700 wpa_printf(MSG_DEBUG
, "P2P: Failed to allocate Action frame "
701 "TX buffer (len=%llu)", (unsigned long long) len
);
704 wpabuf_put_data(wpa_s
->pending_action_tx
, buf
, len
);
705 os_memcpy(wpa_s
->pending_action_src
, src
, ETH_ALEN
);
706 os_memcpy(wpa_s
->pending_action_dst
, dst
, ETH_ALEN
);
707 os_memcpy(wpa_s
->pending_action_bssid
, bssid
, ETH_ALEN
);
708 wpa_s
->pending_action_freq
= freq
;
710 if (freq
!= 0 && wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_OFFCHANNEL_TX
) {
711 struct wpa_supplicant
*iface
;
713 iface
= wpas_get_tx_interface(wpa_s
, wpa_s
->pending_action_src
);
714 wpa_s
->action_tx_wait_time
= wait_time
;
716 return wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
,
717 wait_time
, wpa_s
->pending_action_dst
,
718 wpa_s
->pending_action_src
,
719 wpa_s
->pending_action_bssid
,
720 wpabuf_head(wpa_s
->pending_action_tx
),
721 wpabuf_len(wpa_s
->pending_action_tx
));
725 struct wpa_supplicant
*tx_iface
;
726 tx_iface
= wpas_get_tx_interface(wpa_s
, src
);
727 if (tx_iface
->assoc_freq
== freq
) {
728 wpa_printf(MSG_DEBUG
, "P2P: Already on requested "
729 "channel (TX interface operating channel)");
734 if (wpa_s
->off_channel_freq
== freq
|| freq
== 0) {
735 wpa_printf(MSG_DEBUG
, "P2P: Already on requested channel; "
736 "send Action frame immediately");
737 /* TODO: Would there ever be need to extend the current
738 * duration on the channel? */
739 wpa_s
->pending_action_without_roc
= 1;
740 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
741 eloop_register_timeout(0, 0, wpas_send_action_cb
, wpa_s
, NULL
);
744 wpa_s
->pending_action_without_roc
= 0;
746 if (wpa_s
->roc_waiting_drv_freq
== freq
) {
747 wpa_printf(MSG_DEBUG
, "P2P: Already waiting for driver to get "
748 "to frequency %u MHz; continue waiting to send the "
749 "Action frame", freq
);
753 wpa_printf(MSG_DEBUG
, "P2P: Schedule Action frame to be transmitted "
754 "once the driver gets to the requested channel");
755 if (wait_time
> wpa_s
->max_remain_on_chan
)
756 wait_time
= wpa_s
->max_remain_on_chan
;
757 if (wpa_drv_remain_on_channel(wpa_s
, freq
, wait_time
) < 0) {
758 wpa_printf(MSG_DEBUG
, "P2P: Failed to request driver "
759 "to remain on channel (%u MHz) for Action "
763 wpa_s
->off_channel_freq
= 0;
764 wpa_s
->roc_waiting_drv_freq
= freq
;
770 static void wpas_send_action_done(void *ctx
)
772 struct wpa_supplicant
*wpa_s
= ctx
;
773 wpa_printf(MSG_DEBUG
, "P2P: Action frame sequence done notification");
774 wpabuf_free(wpa_s
->pending_action_tx
);
775 wpa_s
->pending_action_tx
= NULL
;
776 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_OFFCHANNEL_TX
) {
777 if (wpa_s
->action_tx_wait_time
)
778 wpa_drv_send_action_cancel_wait(wpa_s
);
779 wpa_s
->off_channel_freq
= 0;
780 } else if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
781 wpa_drv_cancel_remain_on_channel(wpa_s
);
782 wpa_s
->off_channel_freq
= 0;
783 wpa_s
->roc_waiting_drv_freq
= 0;
788 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
789 struct p2p_go_neg_results
*params
)
791 if (wpa_s
->go_params
== NULL
) {
792 wpa_s
->go_params
= os_malloc(sizeof(*params
));
793 if (wpa_s
->go_params
== NULL
)
796 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
801 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
802 struct p2p_go_neg_results
*res
)
804 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
805 MAC2STR(res
->peer_interface_addr
));
806 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
807 res
->ssid
, res
->ssid_len
);
808 wpa_supplicant_ap_deinit(wpa_s
);
809 wpas_copy_go_neg_results(wpa_s
, res
);
810 if (res
->wps_method
== WPS_PBC
)
811 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
813 u16 dev_pw_id
= DEV_PW_DEFAULT
;
814 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
815 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
816 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
817 wpa_s
->p2p_pin
, 1, dev_pw_id
);
822 static void p2p_go_configured(void *ctx
, void *data
)
824 struct wpa_supplicant
*wpa_s
= ctx
;
825 struct p2p_go_neg_results
*params
= data
;
826 struct wpa_ssid
*ssid
;
828 ssid
= wpa_s
->current_ssid
;
829 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
830 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
831 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
832 wpa_s
->global
->p2p_group_formation
= NULL
;
833 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
834 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
835 "go_dev_addr=" MACSTR
"%s",
837 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
839 params
->passphrase
? params
->passphrase
: "",
840 MAC2STR(wpa_s
->parent
->own_addr
),
841 params
->persistent_group
? " [PERSISTENT]" : "");
842 if (params
->persistent_group
)
843 wpas_p2p_store_persistent_group(
845 wpa_s
->parent
->own_addr
);
846 wpas_p2p_cross_connect_setup(wpa_s
);
847 wpas_p2p_set_group_idle_timeout(wpa_s
);
851 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
852 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
853 params
->peer_interface_addr
)) {
854 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
858 if (params
->wps_method
== WPS_PBC
)
859 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
);
860 else if (wpa_s
->p2p_pin
[0])
861 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
862 wpa_s
->p2p_pin
, NULL
, 0);
863 os_free(wpa_s
->go_params
);
864 wpa_s
->go_params
= NULL
;
868 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
869 struct p2p_go_neg_results
*params
,
872 struct wpa_ssid
*ssid
;
874 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0)
877 ssid
= wpa_config_add_network(wpa_s
->conf
);
881 wpas_notify_network_added(wpa_s
, ssid
);
882 wpa_config_set_network_defaults(ssid
);
885 ssid
->p2p_persistent_group
= params
->persistent_group
;
886 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
888 ssid
->frequency
= params
->freq
;
889 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
891 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
892 ssid
->ssid_len
= params
->ssid_len
;
894 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
895 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
896 ssid
->proto
= WPA_PROTO_RSN
;
897 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
898 ssid
->passphrase
= os_strdup(params
->passphrase
);
900 wpa_s
->ap_configured_cb
= p2p_go_configured
;
901 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
902 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
903 wpa_s
->connect_without_scan
= 1;
904 wpa_s
->reassociate
= 1;
905 wpa_s
->disconnected
= 0;
906 wpa_supplicant_req_scan(wpa_s
, 0, 0);
910 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
911 const struct wpa_supplicant
*src
)
913 struct wpa_config
*d
;
914 const struct wpa_config
*s
;
919 #define C(n) if (s->n) d->n = os_strdup(s->n)
929 d
->p2p_group_idle
= s
->p2p_group_idle
;
930 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
934 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
935 enum wpa_driver_if_type type
)
937 char ifname
[120], force_ifname
[120];
939 if (wpa_s
->pending_interface_name
[0]) {
940 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
941 "- skip creation of a new one");
942 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
943 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
944 "unknown?! ifname='%s'",
945 wpa_s
->pending_interface_name
);
951 os_snprintf(ifname
, sizeof(ifname
), "p2p-%s-%d", wpa_s
->ifname
,
952 wpa_s
->p2p_group_idx
);
953 if (os_strlen(ifname
) >= IFNAMSIZ
&&
954 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
955 /* Try to avoid going over the IFNAMSIZ length limit */
956 os_snprintf(ifname
, sizeof(ifname
), "p2p-%d",
957 wpa_s
->p2p_group_idx
);
959 force_ifname
[0] = '\0';
961 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
963 wpa_s
->p2p_group_idx
++;
965 wpa_s
->pending_interface_type
= type
;
966 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
967 wpa_s
->pending_interface_addr
) < 0) {
968 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
973 if (force_ifname
[0]) {
974 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
976 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
977 sizeof(wpa_s
->pending_interface_name
));
979 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
980 sizeof(wpa_s
->pending_interface_name
));
981 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
982 MACSTR
, wpa_s
->pending_interface_name
,
983 MAC2STR(wpa_s
->pending_interface_addr
));
989 static void wpas_p2p_remove_pending_group_interface(
990 struct wpa_supplicant
*wpa_s
)
992 if (!wpa_s
->pending_interface_name
[0] ||
993 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
994 return; /* No pending virtual interface */
996 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
997 wpa_s
->pending_interface_name
);
998 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
999 wpa_s
->pending_interface_name
);
1000 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1001 wpa_s
->pending_interface_name
[0] = '\0';
1005 static struct wpa_supplicant
*
1006 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1008 struct wpa_interface iface
;
1009 struct wpa_supplicant
*group_wpa_s
;
1011 if (!wpa_s
->pending_interface_name
[0]) {
1012 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1013 if (!wpas_p2p_create_iface(wpa_s
))
1016 * Something has forced us to remove the pending interface; try
1017 * to create a new one and hope for the best that we will get
1018 * the same local address.
1020 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1021 WPA_IF_P2P_CLIENT
) < 0)
1025 os_memset(&iface
, 0, sizeof(iface
));
1026 iface
.ifname
= wpa_s
->pending_interface_name
;
1027 iface
.driver
= wpa_s
->driver
->name
;
1028 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1029 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1030 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1031 if (group_wpa_s
== NULL
) {
1032 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1033 "wpa_supplicant interface");
1036 wpa_s
->pending_interface_name
[0] = '\0';
1037 group_wpa_s
->parent
= wpa_s
;
1038 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1039 P2P_GROUP_INTERFACE_CLIENT
;
1040 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1042 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
1048 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
1051 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1052 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
1053 if (wpa_s
->global
->p2p
)
1054 p2p_group_formation_failed(wpa_s
->global
->p2p
);
1055 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1056 wpa_drv_p2p_group_formation_failed(wpa_s
);
1057 wpas_group_formation_completed(wpa_s
, 0);
1061 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
1063 struct wpa_supplicant
*wpa_s
= ctx
;
1065 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1066 wpa_drv_cancel_remain_on_channel(wpa_s
);
1067 wpa_s
->off_channel_freq
= 0;
1068 wpa_s
->roc_waiting_drv_freq
= 0;
1072 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_FAILURE
"status=%d",
1074 wpas_p2p_remove_pending_group_interface(wpa_s
);
1078 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
);
1080 if (wpa_s
->create_p2p_iface
) {
1081 struct wpa_supplicant
*group_wpa_s
=
1082 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
1083 if (group_wpa_s
== NULL
) {
1084 wpas_p2p_remove_pending_group_interface(wpa_s
);
1087 if (group_wpa_s
!= wpa_s
) {
1088 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
1089 sizeof(group_wpa_s
->p2p_pin
));
1090 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
1092 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1093 wpa_s
->pending_interface_name
[0] = '\0';
1094 group_wpa_s
->p2p_in_provisioning
= 1;
1097 wpas_start_wps_go(group_wpa_s
, res
, 1);
1099 wpas_start_wps_enrollee(group_wpa_s
, res
);
1101 wpa_s
->p2p_in_provisioning
= 1;
1102 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1105 wpas_start_wps_go(wpa_s
, res
, 1);
1107 wpas_start_wps_enrollee(ctx
, res
);
1110 wpa_s
->p2p_long_listen
= 0;
1111 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1113 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1114 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1115 (res
->peer_config_timeout
% 100) * 10000,
1116 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1120 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1122 struct wpa_supplicant
*wpa_s
= ctx
;
1123 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1124 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1128 void wpas_dev_found(void *ctx
, const u8
*addr
, const u8
*dev_addr
,
1129 const u8
*pri_dev_type
, const char *dev_name
,
1130 u16 config_methods
, u8 dev_capab
, u8 group_capab
)
1132 struct wpa_supplicant
*wpa_s
= ctx
;
1133 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1134 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1135 " p2p_dev_addr=" MACSTR
1136 " pri_dev_type=%s name='%s' config_methods=0x%x "
1137 "dev_capab=0x%x group_capab=0x%x",
1138 MAC2STR(addr
), MAC2STR(dev_addr
),
1139 wps_dev_type_bin2str(pri_dev_type
, devtype
, sizeof(devtype
)),
1140 dev_name
, config_methods
, dev_capab
, group_capab
);
1144 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1145 unsigned int duration
,
1146 const struct wpabuf
*probe_resp_ie
)
1148 struct wpa_supplicant
*wpa_s
= ctx
;
1150 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1152 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1153 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1154 "report received Probe Request frames");
1158 wpa_s
->pending_listen_freq
= freq
;
1159 wpa_s
->pending_listen_duration
= duration
;
1161 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1162 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1163 "to remain on channel (%u MHz) for Listen "
1165 wpa_s
->pending_listen_freq
= 0;
1168 wpa_s
->off_channel_freq
= 0;
1169 wpa_s
->roc_waiting_drv_freq
= freq
;
1175 static void wpas_stop_listen(void *ctx
)
1177 struct wpa_supplicant
*wpa_s
= ctx
;
1178 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1179 wpa_drv_cancel_remain_on_channel(wpa_s
);
1180 wpa_s
->off_channel_freq
= 0;
1181 wpa_s
->roc_waiting_drv_freq
= 0;
1183 wpa_drv_probe_req_report(wpa_s
, 0);
1187 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1189 struct wpa_supplicant
*wpa_s
= ctx
;
1190 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
));
1194 static struct p2p_srv_bonjour
*
1195 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1196 const struct wpabuf
*query
)
1198 struct p2p_srv_bonjour
*bsrv
;
1201 len
= wpabuf_len(query
);
1202 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1203 struct p2p_srv_bonjour
, list
) {
1204 if (len
== wpabuf_len(bsrv
->query
) &&
1205 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1213 static struct p2p_srv_upnp
*
1214 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1215 const char *service
)
1217 struct p2p_srv_upnp
*usrv
;
1219 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1220 struct p2p_srv_upnp
, list
) {
1221 if (version
== usrv
->version
&&
1222 os_strcmp(service
, usrv
->service
) == 0)
1229 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1234 if (wpabuf_tailroom(resp
) < 5)
1237 /* Length (to be filled) */
1238 len_pos
= wpabuf_put(resp
, 2);
1239 wpabuf_put_u8(resp
, srv_proto
);
1240 wpabuf_put_u8(resp
, srv_trans_id
);
1242 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1243 /* Response Data: empty */
1244 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1248 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1249 struct wpabuf
*resp
, u8 srv_trans_id
)
1251 struct p2p_srv_bonjour
*bsrv
;
1254 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1256 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1257 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1261 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1262 struct p2p_srv_bonjour
, list
) {
1263 if (wpabuf_tailroom(resp
) <
1264 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1266 /* Length (to be filled) */
1267 len_pos
= wpabuf_put(resp
, 2);
1268 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1269 wpabuf_put_u8(resp
, srv_trans_id
);
1271 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1272 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1273 wpabuf_head(bsrv
->resp
),
1274 wpabuf_len(bsrv
->resp
));
1276 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1277 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1278 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1284 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1285 struct wpabuf
*resp
, u8 srv_trans_id
,
1286 const u8
*query
, size_t query_len
)
1288 struct p2p_srv_bonjour
*bsrv
;
1292 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1294 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1295 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1296 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1301 if (query_len
== 0) {
1302 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1306 if (wpabuf_tailroom(resp
) < 5)
1308 /* Length (to be filled) */
1309 len_pos
= wpabuf_put(resp
, 2);
1310 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1311 wpabuf_put_u8(resp
, srv_trans_id
);
1313 wpabuf_set(&buf
, query
, query_len
);
1314 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, &buf
);
1316 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1320 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1321 /* Response Data: empty */
1322 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1328 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1329 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1330 wpabuf_head(bsrv
->resp
), wpabuf_len(bsrv
->resp
));
1332 if (wpabuf_tailroom(resp
) >=
1333 wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
)) {
1335 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1336 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1338 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1342 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1343 struct wpabuf
*resp
, u8 srv_trans_id
)
1345 struct p2p_srv_upnp
*usrv
;
1348 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1350 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1351 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1355 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1356 struct p2p_srv_upnp
, list
) {
1357 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1360 /* Length (to be filled) */
1361 len_pos
= wpabuf_put(resp
, 2);
1362 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1363 wpabuf_put_u8(resp
, srv_trans_id
);
1366 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1368 wpabuf_put_u8(resp
, usrv
->version
);
1369 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1371 wpabuf_put_str(resp
, usrv
->service
);
1372 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1378 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1379 struct wpabuf
*resp
, u8 srv_trans_id
,
1380 const u8
*query
, size_t query_len
)
1382 struct p2p_srv_upnp
*usrv
;
1388 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1391 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1392 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1393 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1398 if (query_len
== 0) {
1399 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1403 if (wpabuf_tailroom(resp
) < 5)
1406 /* Length (to be filled) */
1407 len_pos
= wpabuf_put(resp
, 2);
1408 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1409 wpabuf_put_u8(resp
, srv_trans_id
);
1412 str
= os_malloc(query_len
);
1415 os_memcpy(str
, query
+ 1, query_len
- 1);
1416 str
[query_len
- 1] = '\0';
1418 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1419 struct p2p_srv_upnp
, list
) {
1420 if (version
!= usrv
->version
)
1423 if (os_strcmp(str
, "ssdp:all") != 0 &&
1424 os_strstr(usrv
->service
, str
) == NULL
)
1427 if (wpabuf_tailroom(resp
) < 2)
1431 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1433 wpabuf_put_u8(resp
, version
);
1435 wpabuf_put_u8(resp
, ',');
1439 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1441 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1443 wpabuf_put_str(resp
, usrv
->service
);
1447 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1450 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1451 /* Response Data: empty */
1454 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1458 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
1459 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
1461 struct wpa_supplicant
*wpa_s
= ctx
;
1462 const u8
*pos
= tlvs
;
1463 const u8
*end
= tlvs
+ tlvs_len
;
1466 struct wpabuf
*resp
;
1467 u8 srv_proto
, srv_trans_id
;
1471 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
1473 buf_len
= 2 * tlvs_len
+ 1;
1474 buf
= os_malloc(buf_len
);
1476 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1477 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
1479 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
1484 if (wpa_s
->p2p_sd_over_ctrl_iface
)
1485 return; /* to be processed by an external program */
1487 resp
= wpabuf_alloc(10000);
1491 while (pos
+ 1 < end
) {
1492 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
1493 slen
= WPA_GET_LE16(pos
);
1495 if (pos
+ slen
> end
|| slen
< 2) {
1496 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
1501 tlv_end
= pos
+ slen
;
1504 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1506 srv_trans_id
= *pos
++;
1507 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1510 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
1511 pos
, tlv_end
- pos
);
1514 if (wpa_s
->force_long_sd
) {
1515 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
1517 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1518 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1522 switch (srv_proto
) {
1523 case P2P_SERV_ALL_SERVICES
:
1524 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
1525 "for all services");
1526 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
1527 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1528 wpa_printf(MSG_DEBUG
, "P2P: No service "
1529 "discovery protocols available");
1530 wpas_sd_add_proto_not_avail(
1531 resp
, P2P_SERV_ALL_SERVICES
,
1535 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1536 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1538 case P2P_SERV_BONJOUR
:
1539 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
1540 pos
, tlv_end
- pos
);
1543 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
1544 pos
, tlv_end
- pos
);
1547 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
1548 "protocol %u", srv_proto
);
1549 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
1558 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
1564 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
1565 const u8
*tlvs
, size_t tlvs_len
)
1567 struct wpa_supplicant
*wpa_s
= ctx
;
1568 const u8
*pos
= tlvs
;
1569 const u8
*end
= tlvs
+ tlvs_len
;
1575 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
1577 if (tlvs_len
> 1500) {
1578 /* TODO: better way for handling this */
1579 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1580 P2P_EVENT_SERV_DISC_RESP MACSTR
1581 " %u <long response: %u bytes>",
1582 MAC2STR(sa
), update_indic
,
1583 (unsigned int) tlvs_len
);
1585 buf_len
= 2 * tlvs_len
+ 1;
1586 buf
= os_malloc(buf_len
);
1588 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1589 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1590 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
1591 MAC2STR(sa
), update_indic
, buf
);
1597 u8 srv_proto
, srv_trans_id
, status
;
1599 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
1600 slen
= WPA_GET_LE16(pos
);
1602 if (pos
+ slen
> end
|| slen
< 3) {
1603 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
1607 tlv_end
= pos
+ slen
;
1610 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1612 srv_trans_id
= *pos
++;
1613 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1616 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
1619 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
1620 pos
, tlv_end
- pos
);
1627 void * wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1628 const struct wpabuf
*tlvs
)
1630 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1631 return (void *) wpa_drv_p2p_sd_request(wpa_s
, dst
, tlvs
);
1632 return p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
1636 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1637 u8 version
, const char *query
)
1639 struct wpabuf
*tlvs
;
1642 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
1645 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
1646 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
1647 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
1648 wpabuf_put_u8(tlvs
, version
);
1649 wpabuf_put_str(tlvs
, query
);
1650 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
1656 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, void *req
)
1658 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1659 return wpa_drv_p2p_sd_cancel_request(wpa_s
, (u64
) req
);
1660 return p2p_sd_cancel_request(wpa_s
->global
->p2p
, req
);
1664 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
1665 const u8
*dst
, u8 dialog_token
,
1666 const struct wpabuf
*resp_tlvs
)
1668 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1669 wpa_drv_p2p_sd_response(wpa_s
, freq
, dst
, dialog_token
,
1673 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
1678 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
1680 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1681 wpa_drv_p2p_service_update(wpa_s
);
1684 p2p_sd_service_update(wpa_s
->global
->p2p
);
1688 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
1690 dl_list_del(&bsrv
->list
);
1691 wpabuf_free(bsrv
->query
);
1692 wpabuf_free(bsrv
->resp
);
1697 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
1699 dl_list_del(&usrv
->list
);
1700 os_free(usrv
->service
);
1705 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
1707 struct p2p_srv_bonjour
*bsrv
, *bn
;
1708 struct p2p_srv_upnp
*usrv
, *un
;
1710 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
1711 struct p2p_srv_bonjour
, list
)
1712 wpas_p2p_srv_bonjour_free(bsrv
);
1714 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
1715 struct p2p_srv_upnp
, list
)
1716 wpas_p2p_srv_upnp_free(usrv
);
1718 wpas_p2p_sd_service_update(wpa_s
);
1722 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
1723 struct wpabuf
*query
, struct wpabuf
*resp
)
1725 struct p2p_srv_bonjour
*bsrv
;
1727 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1730 wpabuf_free(bsrv
->resp
);
1735 bsrv
= os_zalloc(sizeof(*bsrv
));
1738 bsrv
->query
= query
;
1740 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
1742 wpas_p2p_sd_service_update(wpa_s
);
1747 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
1748 const struct wpabuf
*query
)
1750 struct p2p_srv_bonjour
*bsrv
;
1752 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1755 wpas_p2p_srv_bonjour_free(bsrv
);
1756 wpas_p2p_sd_service_update(wpa_s
);
1761 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1762 const char *service
)
1764 struct p2p_srv_upnp
*usrv
;
1766 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
1767 return 0; /* Already listed */
1768 usrv
= os_zalloc(sizeof(*usrv
));
1771 usrv
->version
= version
;
1772 usrv
->service
= os_strdup(service
);
1773 if (usrv
->service
== NULL
) {
1777 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
1779 wpas_p2p_sd_service_update(wpa_s
);
1784 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1785 const char *service
)
1787 struct p2p_srv_upnp
*usrv
;
1789 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
1792 wpas_p2p_srv_upnp_free(usrv
);
1793 wpas_p2p_sd_service_update(wpa_s
);
1798 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
1799 const u8
*peer
, const char *params
)
1801 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
" %08d%s",
1802 MAC2STR(peer
), wps_generate_pin(), params
);
1806 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
1807 const u8
*peer
, const char *params
)
1809 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
"%s",
1810 MAC2STR(peer
), params
);
1814 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
1815 const u8
*dev_addr
, const u8
*pri_dev_type
,
1816 const char *dev_name
, u16 supp_config_methods
,
1817 u8 dev_capab
, u8 group_capab
)
1819 struct wpa_supplicant
*wpa_s
= ctx
;
1820 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1822 u8 empty_dev_type
[8];
1824 if (pri_dev_type
== NULL
) {
1825 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
1826 pri_dev_type
= empty_dev_type
;
1828 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
1829 " pri_dev_type=%s name='%s' config_methods=0x%x "
1830 "dev_capab=0x%x group_capab=0x%x",
1832 wps_dev_type_bin2str(pri_dev_type
, devtype
,
1834 dev_name
, supp_config_methods
, dev_capab
, group_capab
);
1835 params
[sizeof(params
) - 1] = '\0';
1837 if (config_methods
& WPS_CONFIG_DISPLAY
)
1838 wpas_prov_disc_local_display(wpa_s
, peer
, params
);
1839 else if (config_methods
& WPS_CONFIG_KEYPAD
)
1840 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
1841 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1842 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1843 "%s", MAC2STR(peer
), params
);
1847 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
1849 struct wpa_supplicant
*wpa_s
= ctx
;
1850 if (config_methods
& WPS_CONFIG_DISPLAY
)
1851 wpas_prov_disc_local_keypad(wpa_s
, peer
, "");
1852 else if (config_methods
& WPS_CONFIG_KEYPAD
)
1853 wpas_prov_disc_local_display(wpa_s
, peer
, "");
1854 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1855 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
,
1858 if (wpa_s
->pending_pd_before_join
&&
1859 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
1860 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
1861 wpa_s
->pending_pd_before_join
= 0;
1862 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
1863 "join-existing-group operation");
1864 wpas_p2p_join_start(wpa_s
);
1869 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
1870 const u8
*go_dev_addr
, const u8
*ssid
,
1871 size_t ssid_len
, int *go
, u8
*group_bssid
,
1872 int *force_freq
, int persistent_group
)
1874 struct wpa_supplicant
*wpa_s
= ctx
;
1876 u8 cur_bssid
[ETH_ALEN
];
1878 struct wpa_supplicant
*grp
;
1880 if (!persistent_group
) {
1881 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1882 " to join an active group", MAC2STR(sa
));
1883 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
1884 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
1886 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
1887 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
1888 "authorized invitation");
1892 * Do not accept the invitation automatically; notify user and
1895 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1898 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
1900 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
1901 "running persistent group");
1903 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
1907 if (!wpa_s
->conf
->persistent_reconnect
)
1908 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1910 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1911 if (s
->disabled
== 2 &&
1912 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
1913 s
->ssid_len
== ssid_len
&&
1914 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
1919 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1920 " requested reinvocation of an unknown group",
1922 return P2P_SC_FAIL_UNKNOWN_GROUP
;
1925 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
1927 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
1928 wpa_printf(MSG_DEBUG
, "P2P: The only available "
1929 "interface is already in use - reject "
1931 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1933 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
1934 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
1936 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
1938 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
1939 "interface address for the group");
1940 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1942 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
1947 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, cur_bssid
) == 0 &&
1948 wpa_s
->assoc_freq
) {
1949 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
1950 "the channel we are already using");
1951 *force_freq
= wpa_s
->assoc_freq
;
1954 res
= wpa_drv_shared_freq(wpa_s
);
1956 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
1957 "with the channel we are already using on a "
1958 "shared interface");
1962 return P2P_SC_SUCCESS
;
1966 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
1967 const u8
*ssid
, size_t ssid_len
,
1968 const u8
*go_dev_addr
, u8 status
,
1971 struct wpa_supplicant
*wpa_s
= ctx
;
1974 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1975 if (s
->disabled
== 2 &&
1976 s
->ssid_len
== ssid_len
&&
1977 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
1981 if (status
== P2P_SC_SUCCESS
) {
1982 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
1983 " was accepted; op_freq=%d MHz",
1984 MAC2STR(sa
), op_freq
);
1986 wpas_p2p_group_add_persistent(
1987 wpa_s
, s
, s
->mode
== WPAS_MODE_P2P_GO
, 0);
1989 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
1990 wpa_s
->p2p_wps_method
);
1995 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
1996 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
1997 " was rejected (status %u)", MAC2STR(sa
), status
);
2003 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2004 "sa=" MACSTR
" go_dev_addr=" MACSTR
2005 " bssid=" MACSTR
" unknown-network",
2006 MAC2STR(sa
), MAC2STR(go_dev_addr
),
2009 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2010 "sa=" MACSTR
" go_dev_addr=" MACSTR
2012 MAC2STR(sa
), MAC2STR(go_dev_addr
));
2017 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
"sa=" MACSTR
2018 " persistent=%d", MAC2STR(sa
), s
->id
);
2022 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
)
2024 struct wpa_supplicant
*wpa_s
= ctx
;
2025 struct wpa_ssid
*ssid
;
2028 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2029 "status=%d " MACSTR
,
2030 status
, MAC2STR(bssid
));
2032 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2033 "status=%d ", status
);
2036 if (wpa_s
->pending_invite_ssid_id
== -1)
2037 return; /* Invitation to active group */
2039 if (status
!= P2P_SC_SUCCESS
) {
2040 wpas_p2p_remove_pending_group_interface(wpa_s
);
2044 ssid
= wpa_config_get_network(wpa_s
->conf
,
2045 wpa_s
->pending_invite_ssid_id
);
2047 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
2048 "data matching with invitation");
2052 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
2053 ssid
->mode
== WPAS_MODE_P2P_GO
, 0);
2057 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
2058 struct p2p_channels
*chan
)
2062 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
2065 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2066 chan
->reg_class
[cla
].reg_class
= 81;
2067 chan
->reg_class
[cla
].channels
= 11;
2068 for (i
= 0; i
< 11; i
++)
2069 chan
->reg_class
[cla
].channel
[i
] = i
+ 1;
2072 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
2075 /* Operating class 115 - 5 GHz, channels 36-48 */
2076 chan
->reg_class
[cla
].reg_class
= 115;
2077 chan
->reg_class
[cla
].channels
= 4;
2078 chan
->reg_class
[cla
].channel
[0] = 36;
2079 chan
->reg_class
[cla
].channel
[1] = 40;
2080 chan
->reg_class
[cla
].channel
[2] = 44;
2081 chan
->reg_class
[cla
].channel
[3] = 48;
2084 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
2087 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2088 chan
->reg_class
[cla
].reg_class
= 124;
2089 chan
->reg_class
[cla
].channels
= 4;
2090 chan
->reg_class
[cla
].channel
[0] = 149;
2091 chan
->reg_class
[cla
].channel
[1] = 153;
2092 chan
->reg_class
[cla
].channel
[2] = 157;
2093 chan
->reg_class
[cla
].channel
[3] = 161;
2096 chan
->reg_classes
= cla
;
2101 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
2103 enum hostapd_hw_mode mode
)
2107 for (i
= 0; i
< num_modes
; i
++) {
2108 if (modes
[i
].mode
== mode
)
2116 static int has_channel(struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
2120 for (i
= 0; i
< mode
->num_channels
; i
++) {
2121 if (mode
->channels
[i
].chan
== chan
) {
2123 *flags
= mode
->channels
[i
].flag
;
2124 return !(mode
->channels
[i
].flag
&
2125 (HOSTAPD_CHAN_DISABLED
|
2126 HOSTAPD_CHAN_PASSIVE_SCAN
|
2127 HOSTAPD_CHAN_NO_IBSS
|
2128 HOSTAPD_CHAN_RADAR
));
2136 struct p2p_oper_class_map
{
2137 enum hostapd_hw_mode mode
;
2142 enum { BW20
, BW40PLUS
, BW40MINUS
} bw
;
2145 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
2146 struct p2p_channels
*chan
)
2148 struct hostapd_hw_modes
*modes
, *mode
;
2149 u16 num_modes
, flags
;
2151 struct p2p_oper_class_map op_class
[] = {
2152 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
2153 { HOSTAPD_MODE_IEEE80211G
, 82, 14, 14, 1, BW20
},
2154 #if 0 /* Do not enable HT40 on 2 GHz for now */
2155 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
2156 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
2158 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
2159 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
2160 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
2161 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
2162 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
2163 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
2164 { -1, 0, 0, 0, 0, BW20
}
2167 modes
= wpa_drv_get_hw_feature_data(wpa_s
, &num_modes
, &flags
);
2168 if (modes
== NULL
) {
2169 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
2170 "of all supported channels; assume dualband "
2172 return wpas_p2p_default_channels(wpa_s
, chan
);
2177 for (op
= 0; op_class
[op
].op_class
; op
++) {
2178 struct p2p_oper_class_map
*o
= &op_class
[op
];
2180 struct p2p_reg_class
*reg
= NULL
;
2182 mode
= get_mode(modes
, num_modes
, o
->mode
);
2185 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2187 if (!has_channel(mode
, ch
, &flag
))
2189 if (o
->bw
== BW40MINUS
&&
2190 (!(flag
& HOSTAPD_CHAN_HT40MINUS
) ||
2191 !has_channel(mode
, ch
- 4, NULL
)))
2193 if (o
->bw
== BW40PLUS
&&
2194 (!(flag
& HOSTAPD_CHAN_HT40PLUS
) ||
2195 !has_channel(mode
, ch
+ 4, NULL
)))
2198 wpa_printf(MSG_DEBUG
, "P2P: Add operating "
2199 "class %u", o
->op_class
);
2200 reg
= &chan
->reg_class
[cla
];
2202 reg
->reg_class
= o
->op_class
;
2204 reg
->channel
[reg
->channels
] = ch
;
2208 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
2209 reg
->channel
, reg
->channels
);
2213 chan
->reg_classes
= cla
;
2215 ieee80211_sta_free_hw_features(modes
, num_modes
);
2221 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
2224 struct wpa_supplicant
*wpa_s
= ctx
;
2226 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2227 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
2233 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
2238 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2239 * @global: Pointer to global data from wpa_supplicant_init()
2240 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2241 * Returns: 0 on success, -1 on failure
2243 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
2245 struct p2p_config p2p
;
2249 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
2252 #ifdef CONFIG_CLIENT_MLME
2253 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)) {
2254 wpa_s
->mlme
.public_action_cb
= p2p_rx_action_mlme
;
2255 wpa_s
->mlme
.public_action_cb_ctx
= wpa_s
;
2257 #endif /* CONFIG_CLIENT_MLME */
2259 if (wpa_drv_disable_11b_rates(wpa_s
, 1) < 0) {
2260 wpa_printf(MSG_DEBUG
, "P2P: Failed to disable 11b rates");
2261 /* Continue anyway; this is not really a fatal error */
2267 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2268 struct p2p_params params
;
2270 wpa_printf(MSG_DEBUG
, "P2P: Use driver-based P2P management");
2271 os_memset(¶ms
, 0, sizeof(params
));
2272 params
.dev_name
= wpa_s
->conf
->device_name
;
2273 if (wpa_s
->conf
->device_type
&&
2274 wps_dev_type_str2bin(wpa_s
->conf
->device_type
,
2275 params
.pri_dev_type
) < 0) {
2276 wpa_printf(MSG_ERROR
, "P2P: Invalid device_type");
2279 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
2280 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
2282 if (wps_dev_type_str2bin(
2283 wpa_s
->conf
->sec_device_type
[i
],
2284 params
.sec_dev_type
[
2285 params
.num_sec_dev_types
]) < 0) {
2286 wpa_printf(MSG_ERROR
, "P2P: Invalid "
2290 params
.num_sec_dev_types
++;
2291 if (params
.num_sec_dev_types
== DRV_MAX_SEC_DEV_TYPES
)
2294 if (wpa_drv_p2p_set_params(wpa_s
, ¶ms
) < 0)
2300 os_memset(&p2p
, 0, sizeof(p2p
));
2301 p2p
.msg_ctx
= wpa_s
;
2303 p2p
.p2p_scan
= wpas_p2p_scan
;
2304 p2p
.send_action
= wpas_send_action
;
2305 p2p
.send_action_done
= wpas_send_action_done
;
2306 p2p
.go_neg_completed
= wpas_go_neg_completed
;
2307 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
2308 p2p
.dev_found
= wpas_dev_found
;
2309 p2p
.start_listen
= wpas_start_listen
;
2310 p2p
.stop_listen
= wpas_stop_listen
;
2311 p2p
.send_probe_resp
= wpas_send_probe_resp
;
2312 p2p
.sd_request
= wpas_sd_request
;
2313 p2p
.sd_response
= wpas_sd_response
;
2314 p2p
.prov_disc_req
= wpas_prov_disc_req
;
2315 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
2316 p2p
.invitation_process
= wpas_invitation_process
;
2317 p2p
.invitation_received
= wpas_invitation_received
;
2318 p2p
.invitation_result
= wpas_invitation_result
;
2319 p2p
.get_noa
= wpas_get_noa
;
2321 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2322 os_memcpy(p2p
.dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2323 p2p
.dev_name
= wpa_s
->conf
->device_name
;
2325 if (wpa_s
->conf
->p2p_listen_reg_class
&&
2326 wpa_s
->conf
->p2p_listen_channel
) {
2327 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
2328 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
2332 * Pick one of the social channels randomly as the listen
2335 os_get_random((u8
*) &r
, sizeof(r
));
2336 p2p
.channel
= 1 + (r
% 3) * 5;
2338 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
2340 if (wpa_s
->conf
->p2p_oper_reg_class
&&
2341 wpa_s
->conf
->p2p_oper_channel
) {
2342 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
2343 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
2344 p2p
.cfg_op_channel
= 1;
2345 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
2346 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2349 p2p
.op_reg_class
= 81;
2351 * Use random operation channel from (1, 6, 11) if no other
2352 * preference is indicated.
2354 os_get_random((u8
*) &r
, sizeof(r
));
2355 p2p
.op_channel
= 1 + (r
% 3) * 5;
2356 p2p
.cfg_op_channel
= 0;
2357 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
2358 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2360 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
2361 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
2362 p2p
.country
[2] = 0x04;
2364 os_memcpy(p2p
.country
, "XX\x04", 3);
2366 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
)) {
2367 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
2372 if (wpa_s
->conf
->device_type
&&
2373 wps_dev_type_str2bin(wpa_s
->conf
->device_type
, p2p
.pri_dev_type
) <
2375 wpa_printf(MSG_ERROR
, "P2P: Invalid device_type");
2379 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
2380 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
2382 if (wps_dev_type_str2bin(
2383 wpa_s
->conf
->sec_device_type
[i
],
2384 p2p
.sec_dev_type
[p2p
.num_sec_dev_types
]) < 0) {
2385 wpa_printf(MSG_ERROR
, "P2P: Invalid sec_device_type");
2388 p2p
.num_sec_dev_types
++;
2389 if (p2p
.num_sec_dev_types
== P2P_SEC_DEVICE_TYPES
)
2393 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
2394 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
2396 p2p
.max_peers
= 100;
2398 if (wpa_s
->conf
->p2p_ssid_postfix
) {
2399 p2p
.ssid_postfix_len
=
2400 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
2401 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
2402 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
2403 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
2404 p2p
.ssid_postfix_len
);
2407 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
2409 global
->p2p
= p2p_init(&p2p
);
2410 if (global
->p2p
== NULL
)
2418 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2419 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2421 * This function deinitialize per-interface P2P data.
2423 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
2425 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
2426 wpa_drv_probe_req_report(wpa_s
, 0);
2427 os_free(wpa_s
->go_params
);
2428 wpa_s
->go_params
= NULL
;
2429 wpabuf_free(wpa_s
->pending_action_tx
);
2430 wpa_s
->pending_action_tx
= NULL
;
2431 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
2432 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2433 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2434 wpa_s
->p2p_long_listen
= 0;
2435 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
2436 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
2437 wpas_p2p_remove_pending_group_interface(wpa_s
);
2439 /* TODO: remove group interface from the driver if this wpa_s instance
2440 * is on top of a P2P group interface */
2445 * wpas_p2p_deinit_global - Deinitialize global P2P module
2446 * @global: Pointer to global data from wpa_supplicant_init()
2448 * This function deinitializes the global (per device) P2P module.
2450 void wpas_p2p_deinit_global(struct wpa_global
*global
)
2452 struct wpa_supplicant
*wpa_s
, *tmp
;
2455 if (global
->p2p
== NULL
)
2458 /* Remove remaining P2P group interfaces */
2459 wpa_s
= global
->ifaces
;
2461 wpas_p2p_service_flush(wpa_s
);
2462 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
2463 wpa_s
= wpa_s
->next
;
2465 enum wpa_driver_if_type type
;
2466 tmp
= global
->ifaces
;
2469 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
2474 ifname
= os_strdup(tmp
->ifname
);
2475 type
= wpas_p2p_if_type(tmp
->p2p_group_interface
);
2476 wpa_supplicant_remove_iface(global
, tmp
);
2478 wpa_drv_if_remove(wpa_s
, type
, ifname
);
2483 * Deinit GO data on any possibly remaining interface (if main
2484 * interface is used as GO).
2486 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2487 if (wpa_s
->ap_iface
)
2488 wpas_p2p_group_deinit(wpa_s
);
2491 p2p_deinit(global
->p2p
);
2496 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
2498 if (wpa_s
->drv_flags
&
2499 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
2500 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
2501 return 1; /* P2P group requires a new interface in every case
2503 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
2504 return 0; /* driver does not support concurrent operations */
2505 if (wpa_s
->global
->ifaces
->next
)
2506 return 1; /* more that one interface already in use */
2507 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
2508 return 1; /* this interface is already in use */
2513 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
2514 const u8
*peer_addr
,
2515 enum p2p_wps_method wps_method
,
2516 int go_intent
, const u8
*own_interface_addr
,
2517 unsigned int force_freq
, int persistent_group
)
2519 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2520 return wpa_drv_p2p_connect(wpa_s
, peer_addr
, wps_method
,
2521 go_intent
, own_interface_addr
,
2522 force_freq
, persistent_group
);
2525 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2526 go_intent
, own_interface_addr
, force_freq
,
2531 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
2532 const u8
*peer_addr
,
2533 enum p2p_wps_method wps_method
,
2534 int go_intent
, const u8
*own_interface_addr
,
2535 unsigned int force_freq
, int persistent_group
)
2537 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2540 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2541 go_intent
, own_interface_addr
, force_freq
,
2546 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
2548 wpa_s
->p2p_join_scan_count
++;
2549 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
2550 wpa_s
->p2p_join_scan_count
);
2551 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
2552 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
2553 " for join operationg - stop join attempt",
2554 MAC2STR(wpa_s
->pending_join_iface_addr
));
2555 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2556 wpa_msg(wpa_s
->parent
, MSG_INFO
,
2557 P2P_EVENT_GROUP_FORMATION_FAILURE
);
2562 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
2563 struct wpa_scan_results
*scan_res
)
2565 struct wpa_bss
*bss
;
2567 u8 iface_addr
[ETH_ALEN
];
2569 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2571 if (wpa_s
->global
->p2p_disabled
)
2574 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for join",
2575 scan_res
? (int) scan_res
->num
: -1);
2578 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
2580 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2581 wpa_s
->pending_join_iface_addr
);
2583 p2p_get_interface_addr(wpa_s
->global
->p2p
,
2584 wpa_s
->pending_join_dev_addr
,
2586 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
2588 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
2589 "address for join from " MACSTR
" to " MACSTR
2590 " based on newly discovered P2P peer entry",
2591 MAC2STR(wpa_s
->pending_join_iface_addr
),
2592 MAC2STR(iface_addr
));
2593 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
2596 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2597 wpa_s
->pending_join_iface_addr
);
2600 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2601 "from P2P peer table: %d MHz", freq
);
2603 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
2606 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2607 "from BSS table: %d MHz", freq
);
2612 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
2613 "prior to joining an existing group (GO " MACSTR
2615 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
2616 wpa_s
->pending_pd_before_join
= 1;
2618 switch (wpa_s
->pending_join_wps_method
) {
2620 case WPS_PIN_DISPLAY
:
2621 method
= WPS_CONFIG_KEYPAD
;
2623 case WPS_PIN_KEYPAD
:
2624 method
= WPS_CONFIG_DISPLAY
;
2627 method
= WPS_CONFIG_PUSHBUTTON
;
2634 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
2635 wpa_s
->pending_join_dev_addr
, method
, 1)
2637 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
2638 "Discovery Request before joining an "
2640 wpa_s
->pending_pd_before_join
= 0;
2645 * Actual join operation will be started from the Action frame
2646 * TX status callback.
2651 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
2652 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2653 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2654 wpas_p2p_check_join_scan_limit(wpa_s
);
2658 /* Start join operation immediately */
2659 wpas_p2p_join_start(wpa_s
);
2663 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
2665 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
2667 struct wpa_driver_scan_params params
;
2668 struct wpabuf
*wps_ie
, *ies
;
2670 os_memset(¶ms
, 0, sizeof(params
));
2672 /* P2P Wildcard SSID */
2673 params
.num_ssids
= 1;
2674 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
2675 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
2677 wpa_s
->wps
->dev
.p2p
= 1;
2678 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
2680 if (wps_ie
== NULL
) {
2681 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2685 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
2687 wpabuf_free(wps_ie
);
2688 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2691 wpabuf_put_buf(ies
, wps_ie
);
2692 wpabuf_free(wps_ie
);
2694 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
2696 params
.extra_ies
= wpabuf_head(ies
);
2697 params
.extra_ies_len
= wpabuf_len(ies
);
2700 * Run a scan to update BSS table and start Provision Discovery once
2701 * the new scan results become available.
2703 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
2704 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
2705 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
2707 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
2712 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
2714 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2715 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2716 wpas_p2p_check_join_scan_limit(wpa_s
);
2721 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
2722 const u8
*dev_addr
, enum p2p_wps_method wps_method
)
2724 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
2725 MACSTR
" dev " MACSTR
")",
2726 MAC2STR(iface_addr
), MAC2STR(dev_addr
));
2728 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
2729 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
2730 wpa_s
->pending_join_wps_method
= wps_method
;
2732 /* Make sure we are not running find during connection establishment */
2733 wpas_p2p_stop_find(wpa_s
);
2735 wpa_s
->p2p_join_scan_count
= 0;
2736 wpas_p2p_join_scan(wpa_s
, NULL
);
2741 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
2743 struct wpa_supplicant
*group
;
2744 struct p2p_go_neg_results res
;
2746 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
2749 if (group
!= wpa_s
) {
2750 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
2751 sizeof(group
->p2p_pin
));
2752 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
2755 group
->p2p_in_provisioning
= 1;
2757 os_memset(&res
, 0, sizeof(res
));
2758 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
2760 res
.wps_method
= wpa_s
->pending_join_wps_method
;
2761 wpas_start_wps_enrollee(group
, &res
);
2764 * Allow a longer timeout for join-a-running-group than normal 15
2765 * second group formation timeout since the GO may not have authorized
2766 * our connection yet.
2768 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2769 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
2777 * wpas_p2p_connect - Request P2P Group Formation to be started
2778 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2779 * @peer_addr: Address of the peer P2P Device
2780 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2781 * @persistent_group: Whether to create a persistent group
2782 * @join: Whether to join an existing group (as a client) instead of starting
2783 * Group Owner negotiation; @peer_addr is BSSID in that case
2784 * @auth: Whether to only authorize the connection instead of doing that and
2785 * initiating Group Owner negotiation
2786 * @go_intent: GO Intent or -1 to use default
2787 * @freq: Frequency for the group or 0 for auto-selection
2788 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2789 * failure, -2 on failure due to channel not currently available,
2790 * -3 if forced channel is not supported
2792 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
2793 const char *pin
, enum p2p_wps_method wps_method
,
2794 int persistent_group
, int join
, int auth
, int go_intent
,
2797 int force_freq
= 0, oper_freq
= 0;
2800 enum wpa_driver_if_type iftype
;
2803 go_intent
= wpa_s
->conf
->p2p_go_intent
;
2806 wpa_s
->p2p_long_listen
= 0;
2808 wpa_s
->p2p_wps_method
= wps_method
;
2811 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
2812 else if (wps_method
== WPS_PIN_DISPLAY
) {
2813 ret
= wps_generate_pin();
2814 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
2816 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
2819 wpa_s
->p2p_pin
[0] = '\0';
2822 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
2824 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
2825 "connect a running group from " MACSTR
,
2826 MAC2STR(peer_addr
));
2827 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
2830 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
2831 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
2833 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
2834 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
2837 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
) <
2843 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
2845 oper_freq
= wpa_s
->assoc_freq
;
2847 oper_freq
= wpa_drv_shared_freq(wpa_s
);
2853 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
2854 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
2855 "(%u MHz) is not supported for P2P uses",
2860 if (oper_freq
> 0 && freq
!= oper_freq
&&
2861 !(wpa_s
->drv_flags
&
2862 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2863 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2864 "on %u MHz while connected on another "
2865 "channel (%u MHz)", freq
, oper_freq
);
2868 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2869 "requested channel (%u MHz)", freq
);
2871 } else if (oper_freq
> 0 &&
2872 !p2p_supported_freq(wpa_s
->global
->p2p
, oper_freq
)) {
2873 if (!(wpa_s
->drv_flags
&
2874 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2875 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2876 "while connected on non-P2P supported "
2877 "channel (%u MHz)", oper_freq
);
2880 wpa_printf(MSG_DEBUG
, "P2P: Current operating channel "
2881 "(%u MHz) not available for P2P - try to use "
2882 "another channel", oper_freq
);
2884 } else if (oper_freq
> 0) {
2885 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2886 "channel we are already using (%u MHz) on another "
2887 "interface", oper_freq
);
2888 force_freq
= oper_freq
;
2891 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
2893 if (!wpa_s
->create_p2p_iface
) {
2895 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2896 go_intent
, wpa_s
->own_addr
,
2897 force_freq
, persistent_group
)
2902 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
2903 go_intent
, wpa_s
->own_addr
,
2904 force_freq
, persistent_group
) < 0)
2909 /* Prepare to add a new interface for the group */
2910 iftype
= WPA_IF_P2P_GROUP
;
2912 iftype
= WPA_IF_P2P_CLIENT
;
2913 else if (go_intent
== 15)
2914 iftype
= WPA_IF_P2P_GO
;
2915 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
2916 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2917 "interface for the group");
2922 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2924 wpa_s
->pending_interface_addr
,
2925 force_freq
, persistent_group
) < 0)
2929 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
, go_intent
,
2930 wpa_s
->pending_interface_addr
,
2931 force_freq
, persistent_group
) < 0) {
2932 wpas_p2p_remove_pending_group_interface(wpa_s
);
2940 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2941 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2942 * @freq: Frequency of the channel in MHz
2943 * @duration: Duration of the stay on the channel in milliseconds
2945 * This callback is called when the driver indicates that it has started the
2946 * requested remain-on-channel duration.
2948 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
2949 unsigned int freq
, unsigned int duration
)
2951 wpa_s
->roc_waiting_drv_freq
= 0;
2952 wpa_s
->off_channel_freq
= freq
;
2953 wpas_send_action_cb(wpa_s
, NULL
);
2954 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
2955 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
2956 wpa_s
->pending_listen_duration
);
2957 wpa_s
->pending_listen_freq
= 0;
2962 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
2963 unsigned int timeout
)
2965 /* Limit maximum Listen state time based on driver limitation. */
2966 if (timeout
> wpa_s
->max_remain_on_chan
)
2967 timeout
= wpa_s
->max_remain_on_chan
;
2969 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2970 return wpa_drv_p2p_listen(wpa_s
, timeout
);
2972 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
2977 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2978 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2979 * @freq: Frequency of the channel in MHz
2981 * This callback is called when the driver indicates that a remain-on-channel
2982 * operation has been completed, i.e., the duration on the requested channel
2985 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
2988 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
2989 "(p2p_long_listen=%d pending_action_tx=%p)",
2990 wpa_s
->p2p_long_listen
, wpa_s
->pending_action_tx
);
2991 wpa_s
->off_channel_freq
= 0;
2992 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
2993 return; /* P2P module started a new operation */
2994 if (wpa_s
->pending_action_tx
)
2996 if (wpa_s
->p2p_long_listen
> 0)
2997 wpa_s
->p2p_long_listen
-= 5;
2998 if (wpa_s
->p2p_long_listen
> 0) {
2999 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
3000 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
* 1000);
3006 * wpas_p2p_group_remove - Remove a P2P group
3007 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3008 * @ifname: Network interface name of the group interface or "*" to remove all
3010 * Returns: 0 on success, -1 on failure
3012 * This function is used to remove a P2P group. This can be used to disconnect
3013 * from a group in which the local end is a P2P Client or to end a P2P Group in
3014 * case the local end is the Group Owner. If a virtual network interface was
3015 * created for this group, that interface will be removed. Otherwise, only the
3016 * configured P2P group network will be removed from the interface.
3018 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
3020 struct wpa_global
*global
= wpa_s
->global
;
3022 if (os_strcmp(ifname
, "*") == 0) {
3023 struct wpa_supplicant
*prev
;
3024 wpa_s
= global
->ifaces
;
3027 wpa_s
= wpa_s
->next
;
3028 prev
->removal_reason
= P2P_GROUP_REMOVAL_REQUESTED
;
3029 wpas_p2p_group_delete(prev
);
3034 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3035 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3042 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_REQUESTED
;
3043 wpas_p2p_group_delete(wpa_s
);
3049 static void wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
3050 struct p2p_go_neg_results
*params
,
3056 os_memset(params
, 0, sizeof(*params
));
3057 params
->role_go
= 1;
3059 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
3060 "frequency %d MHz", freq
);
3061 params
->freq
= freq
;
3062 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
3063 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
3064 wpa_s
->conf
->p2p_oper_channel
<= 11) {
3065 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3066 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3067 "frequency %d MHz", params
->freq
);
3068 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
3069 wpa_s
->conf
->p2p_oper_reg_class
== 118) {
3070 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3071 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3072 "frequency %d MHz", params
->freq
);
3073 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3074 wpa_s
->best_overall_freq
> 0 &&
3075 p2p_supported_freq(wpa_s
->global
->p2p
,
3076 wpa_s
->best_overall_freq
)) {
3077 params
->freq
= wpa_s
->best_overall_freq
;
3078 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
3079 "channel %d MHz", params
->freq
);
3080 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3081 wpa_s
->best_24_freq
> 0 &&
3082 p2p_supported_freq(wpa_s
->global
->p2p
,
3083 wpa_s
->best_24_freq
)) {
3084 params
->freq
= wpa_s
->best_24_freq
;
3085 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
3086 "channel %d MHz", params
->freq
);
3087 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3088 wpa_s
->best_5_freq
> 0 &&
3089 p2p_supported_freq(wpa_s
->global
->p2p
,
3090 wpa_s
->best_5_freq
)) {
3091 params
->freq
= wpa_s
->best_5_freq
;
3092 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
3093 "channel %d MHz", params
->freq
);
3095 params
->freq
= 2412;
3096 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
3097 "known)", params
->freq
);
3100 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
3101 wpa_s
->assoc_freq
&& !freq
) {
3102 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3104 params
->freq
= wpa_s
->assoc_freq
;
3107 res
= wpa_drv_shared_freq(wpa_s
);
3108 if (res
> 0 && !freq
) {
3109 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3110 "already using on a shared interface");
3116 static struct wpa_supplicant
*
3117 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
3120 struct wpa_supplicant
*group_wpa_s
;
3122 if (!wpas_p2p_create_iface(wpa_s
))
3125 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
3126 WPA_IF_P2P_CLIENT
) < 0)
3128 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
3129 if (group_wpa_s
== NULL
) {
3130 wpas_p2p_remove_pending_group_interface(wpa_s
);
3139 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3140 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3141 * @persistent_group: Whether to create a persistent group
3142 * @freq: Frequency for the group or 0 to indicate no hardcoding
3143 * Returns: 0 on success, -1 on failure
3145 * This function creates a new P2P group with the local end as the Group Owner,
3146 * i.e., without using Group Owner Negotiation.
3148 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
3151 struct p2p_go_neg_results params
;
3155 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
3157 if (wpa_s
->best_24_freq
> 0 &&
3158 p2p_supported_freq(wpa_s
->global
->p2p
,
3159 wpa_s
->best_24_freq
)) {
3160 freq
= wpa_s
->best_24_freq
;
3161 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
3162 "channel: %d MHz", freq
);
3164 os_get_random((u8
*) &r
, sizeof(r
));
3165 freq
= 2412 + (r
% 3) * 25;
3166 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
3167 "channel: %d MHz", freq
);
3172 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
3174 if (wpa_s
->best_5_freq
> 0 &&
3175 p2p_supported_freq(wpa_s
->global
->p2p
,
3176 wpa_s
->best_5_freq
)) {
3177 freq
= wpa_s
->best_5_freq
;
3178 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
3179 "channel: %d MHz", freq
);
3181 os_get_random((u8
*) &r
, sizeof(r
));
3182 freq
= 5180 + (r
% 4) * 20;
3183 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3184 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
3185 "5 GHz channel for P2P group");
3188 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
3189 "channel: %d MHz", freq
);
3193 if (freq
> 0 && !p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3194 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
3195 "(%u MHz) is not supported for P2P uses",
3200 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
3201 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
3202 params
.persistent_group
= persistent_group
;
3204 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
3207 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3213 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
3214 struct wpa_ssid
*params
, int addr_allocated
)
3216 struct wpa_ssid
*ssid
;
3218 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
3222 wpa_supplicant_ap_deinit(wpa_s
);
3224 ssid
= wpa_config_add_network(wpa_s
->conf
);
3227 wpas_notify_network_added(wpa_s
, ssid
);
3228 wpa_config_set_network_defaults(ssid
);
3229 ssid
->temporary
= 1;
3230 ssid
->proto
= WPA_PROTO_RSN
;
3231 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
3232 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
3233 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
3234 ssid
->ssid
= os_malloc(params
->ssid_len
);
3235 if (ssid
->ssid
== NULL
) {
3236 wpas_notify_network_removed(wpa_s
, ssid
);
3237 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
3240 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
3241 ssid
->ssid_len
= params
->ssid_len
;
3242 ssid
->p2p_group
= 1;
3243 if (params
->psk_set
) {
3244 os_memcpy(ssid
->psk
, params
->psk
, 32);
3247 if (params
->passphrase
)
3248 ssid
->passphrase
= os_strdup(params
->passphrase
);
3250 wpa_supplicant_select_network(wpa_s
, ssid
);
3252 wpa_s
->show_group_started
= 1;
3258 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
3259 struct wpa_ssid
*ssid
, int addr_allocated
,
3262 struct p2p_go_neg_results params
;
3265 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
3268 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
3269 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
3270 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
3275 /* Make sure we are not running find during connection establishment */
3276 wpas_p2p_stop_find(wpa_s
);
3278 if (ssid
->mode
== WPAS_MODE_INFRA
)
3279 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
3281 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
3284 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
3287 if (ssid
->passphrase
== NULL
||
3288 os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
3289 wpa_printf(MSG_DEBUG
, "P2P: Invalid passphrase in persistent "
3293 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
3294 sizeof(params
.passphrase
));
3295 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
3296 params
.ssid_len
= ssid
->ssid_len
;
3297 params
.persistent_group
= 1;
3299 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
3303 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3309 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
3310 struct wpabuf
*proberesp_ies
)
3312 struct wpa_supplicant
*wpa_s
= ctx
;
3313 if (wpa_s
->ap_iface
) {
3314 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
3316 wpabuf_free(hapd
->p2p_beacon_ie
);
3317 hapd
->p2p_beacon_ie
= beacon_ies
;
3319 wpabuf_free(hapd
->p2p_probe_resp_ie
);
3320 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
3322 wpabuf_free(beacon_ies
);
3323 wpabuf_free(proberesp_ies
);
3325 wpa_supplicant_ap_update_beacon(wpa_s
);
3329 static void wpas_p2p_idle_update(void *ctx
, int idle
)
3331 struct wpa_supplicant
*wpa_s
= ctx
;
3332 if (!wpa_s
->ap_iface
)
3334 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
3336 wpas_p2p_set_group_idle_timeout(wpa_s
);
3338 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3342 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
3343 int persistent_group
,
3344 int group_formation
)
3346 struct p2p_group
*group
;
3347 struct p2p_group_config
*cfg
;
3349 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3352 cfg
= os_zalloc(sizeof(*cfg
));
3356 cfg
->persistent_group
= persistent_group
;
3357 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
3358 if (wpa_s
->max_stations
&&
3359 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
3360 cfg
->max_clients
= wpa_s
->max_stations
;
3362 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
3363 cfg
->cb_ctx
= wpa_s
;
3364 cfg
->ie_update
= wpas_p2p_ie_update
;
3365 cfg
->idle_update
= wpas_p2p_idle_update
;
3367 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
3370 if (!group_formation
)
3371 p2p_group_notif_formation_done(group
);
3372 wpa_s
->p2p_group
= group
;
3377 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3380 if (!wpa_s
->p2p_in_provisioning
) {
3381 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
3382 "provisioning not in progress");
3386 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
3388 if (wpa_s
->global
->p2p
)
3389 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
3390 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3391 wpa_drv_wps_success_cb(wpa_s
, peer_addr
);
3392 wpas_group_formation_completed(wpa_s
, 1);
3396 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3397 const char *config_method
)
3401 if (os_strcmp(config_method
, "display") == 0)
3402 config_methods
= WPS_CONFIG_DISPLAY
;
3403 else if (os_strcmp(config_method
, "keypad") == 0)
3404 config_methods
= WPS_CONFIG_KEYPAD
;
3405 else if (os_strcmp(config_method
, "pbc") == 0 ||
3406 os_strcmp(config_method
, "pushbutton") == 0)
3407 config_methods
= WPS_CONFIG_PUSHBUTTON
;
3411 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3412 return wpa_drv_p2p_prov_disc_req(wpa_s
, peer_addr
,
3416 if (wpa_s
->global
->p2p
== NULL
)
3419 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
3424 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
3427 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
3431 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
3433 if (!wpa_s
->pending_action_tx
)
3436 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
3437 "operation request");
3438 wpabuf_free(wpa_s
->pending_action_tx
);
3439 wpa_s
->pending_action_tx
= NULL
;
3443 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
3444 enum p2p_discovery_type type
)
3446 wpas_p2p_clear_pending_action_tx(wpa_s
);
3447 wpa_s
->p2p_long_listen
= 0;
3449 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3450 return wpa_drv_p2p_find(wpa_s
, timeout
, type
);
3452 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
);
3456 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
3458 wpas_p2p_clear_pending_action_tx(wpa_s
);
3459 wpa_s
->p2p_long_listen
= 0;
3460 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3461 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3463 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3464 wpa_drv_p2p_stop_find(wpa_s
);
3468 p2p_stop_find(wpa_s
->global
->p2p
);
3470 wpas_p2p_remove_pending_group_interface(wpa_s
);
3474 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
3476 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3477 wpa_s
->p2p_long_listen
= 0;
3481 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
3485 wpas_p2p_clear_pending_action_tx(wpa_s
);
3489 * This is a request for unlimited Listen state. However, at
3490 * least for now, this is mapped to a Listen state for one
3495 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3496 wpa_s
->p2p_long_listen
= 0;
3498 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
3499 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
3500 wpa_s
->p2p_long_listen
= timeout
;
3501 eloop_register_timeout(timeout
, 0,
3502 wpas_p2p_long_listen_timeout
,
3510 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
3511 u8
*buf
, size_t len
, int p2p_group
)
3513 struct wpabuf
*p2p_ie
;
3516 if (wpa_s
->global
->p2p_disabled
)
3518 if (wpa_s
->global
->p2p
== NULL
)
3523 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
3524 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
3526 wpabuf_free(p2p_ie
);
3532 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
3533 const u8
*ie
, size_t ie_len
)
3535 if (wpa_s
->global
->p2p_disabled
)
3537 if (wpa_s
->global
->p2p
== NULL
)
3540 return p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, ie
, ie_len
);
3544 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
3545 const u8
*sa
, const u8
*bssid
,
3546 u8 category
, const u8
*data
, size_t len
, int freq
)
3548 if (wpa_s
->global
->p2p_disabled
)
3550 if (wpa_s
->global
->p2p
== NULL
)
3553 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
3558 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
3560 if (wpa_s
->global
->p2p_disabled
)
3562 if (wpa_s
->global
->p2p
== NULL
)
3565 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
3569 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
3571 p2p_group_deinit(wpa_s
->p2p_group
);
3572 wpa_s
->p2p_group
= NULL
;
3576 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
3578 wpa_s
->p2p_long_listen
= 0;
3580 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3581 return wpa_drv_p2p_reject(wpa_s
, addr
);
3583 return p2p_reject(wpa_s
->global
->p2p
, addr
);
3587 /* Invite to reinvoke a persistent group */
3588 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3589 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
)
3591 enum p2p_invite_role role
;
3594 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3595 role
= P2P_INVITE_ROLE_GO
;
3596 if (peer_addr
== NULL
) {
3597 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
3598 "address in invitation command");
3601 if (wpas_p2p_create_iface(wpa_s
)) {
3602 if (wpas_p2p_add_group_interface(wpa_s
,
3603 WPA_IF_P2P_GO
) < 0) {
3604 wpa_printf(MSG_ERROR
, "P2P: Failed to "
3605 "allocate a new interface for the "
3609 bssid
= wpa_s
->pending_interface_addr
;
3611 bssid
= wpa_s
->own_addr
;
3613 role
= P2P_INVITE_ROLE_CLIENT
;
3614 peer_addr
= ssid
->bssid
;
3616 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
3618 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3619 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3620 ssid
->ssid
, ssid
->ssid_len
,
3623 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3624 ssid
->ssid
, ssid
->ssid_len
, 0, go_dev_addr
, 1);
3628 /* Invite to join an active group */
3629 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
3630 const u8
*peer_addr
, const u8
*go_dev_addr
)
3632 struct wpa_global
*global
= wpa_s
->global
;
3633 enum p2p_invite_role role
;
3635 struct wpa_ssid
*ssid
;
3637 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3638 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3641 if (wpa_s
== NULL
) {
3642 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
3646 ssid
= wpa_s
->current_ssid
;
3648 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
3653 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3654 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
3655 bssid
= wpa_s
->own_addr
;
3656 if (go_dev_addr
== NULL
)
3657 go_dev_addr
= wpa_s
->parent
->own_addr
;
3659 role
= P2P_INVITE_ROLE_CLIENT
;
3660 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
3661 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
3662 "invite to current group");
3665 bssid
= wpa_s
->bssid
;
3666 if (go_dev_addr
== NULL
&&
3667 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
3668 go_dev_addr
= wpa_s
->go_dev_addr
;
3670 wpa_s
->parent
->pending_invite_ssid_id
= -1;
3672 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3673 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3674 ssid
->ssid
, ssid
->ssid_len
,
3677 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3678 ssid
->ssid
, ssid
->ssid_len
, wpa_s
->assoc_freq
,
3683 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
3685 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
3686 const char *ssid_txt
;
3687 u8 go_dev_addr
[ETH_ALEN
];
3690 if (!wpa_s
->show_group_started
|| !ssid
)
3693 wpa_s
->show_group_started
= 0;
3695 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
3696 os_memset(go_dev_addr
, 0, ETH_ALEN
);
3697 if (ssid
->bssid_set
)
3698 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
3699 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
3701 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
3703 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
3704 wpa_s
->global
->p2p_group_formation
= NULL
;
3706 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
3708 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
3709 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3710 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3712 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
3713 MAC2STR(go_dev_addr
),
3714 persistent
? " [PERSISTENT]" : "");
3716 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3717 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3718 "go_dev_addr=" MACSTR
"%s",
3719 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
,
3720 ssid
->passphrase
? ssid
->passphrase
: "",
3721 MAC2STR(go_dev_addr
),
3722 persistent
? " [PERSISTENT]" : "");
3726 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
3731 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
3732 u32 interval1
, u32 duration2
, u32 interval2
)
3734 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3737 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
3738 wpa_s
->current_ssid
== NULL
||
3739 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
3742 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
3743 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
3744 duration1
, interval1
, duration2
, interval2
);
3748 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
3749 unsigned int interval
)
3751 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3754 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
3758 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
3760 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3762 if (wpa_s
->conf
->p2p_group_idle
== 0) {
3763 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
3768 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
3770 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_IDLE_TIMEOUT
;
3771 wpas_p2p_group_delete(wpa_s
);
3775 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
3777 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3778 if (wpa_s
->conf
->p2p_group_idle
== 0)
3781 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
3784 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
3785 wpa_s
->conf
->p2p_group_idle
);
3786 eloop_register_timeout(wpa_s
->conf
->p2p_group_idle
, 0,
3787 wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3791 void wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3792 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3794 if (wpa_s
->global
->p2p_disabled
)
3796 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3799 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3803 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3804 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3806 if (wpa_s
->global
->p2p_disabled
)
3808 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3811 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3815 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
3817 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
3822 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
3825 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
3826 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
3828 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
) {
3830 if (wpa_s
->conf
->device_type
) {
3831 if (wps_dev_type_str2bin(wpa_s
->conf
->device_type
,
3832 pri_dev_type
) < 0) {
3833 wpa_printf(MSG_ERROR
, "P2P: Invalid "
3836 p2p_set_pri_dev_type(p2p
, pri_dev_type
);
3840 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
) {
3841 u8 sec_dev_type
[P2P_SEC_DEVICE_TYPES
][8];
3844 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
3845 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
3847 if (wps_dev_type_str2bin(
3848 wpa_s
->conf
->sec_device_type
[i
],
3849 sec_dev_type
[num
]) < 0) {
3850 wpa_printf(MSG_ERROR
, "P2P: Invalid "
3855 if (num
== P2P_SEC_DEVICE_TYPES
)
3858 p2p_set_sec_dev_types(p2p
, (void *) sec_dev_type
, num
);
3861 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
3862 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
3864 country
[0] = wpa_s
->conf
->country
[0];
3865 country
[1] = wpa_s
->conf
->country
[1];
3867 p2p_set_country(p2p
, country
);
3870 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
3871 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
3872 wpa_s
->conf
->p2p_ssid_postfix
?
3873 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
3877 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
3878 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
3882 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
3885 if (!wpa_s
->ap_iface
)
3887 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
3892 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
3894 if (wpa_s
->global
->p2p_disabled
)
3896 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3899 wpa_s
->global
->cross_connection
= enabled
;
3900 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
3903 struct wpa_supplicant
*iface
;
3905 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
3907 if (iface
->cross_connect_enabled
== 0)
3910 iface
->cross_connect_enabled
= 0;
3911 iface
->cross_connect_in_use
= 0;
3912 wpa_msg(iface
->parent
, MSG_INFO
,
3913 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
3914 iface
->ifname
, iface
->cross_connect_uplink
);
3922 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
3924 struct wpa_supplicant
*iface
;
3926 if (!uplink
->global
->cross_connection
)
3929 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
3930 if (!iface
->cross_connect_enabled
)
3932 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
3935 if (iface
->ap_iface
== NULL
)
3937 if (iface
->cross_connect_in_use
)
3940 iface
->cross_connect_in_use
= 1;
3941 wpa_msg(iface
->parent
, MSG_INFO
,
3942 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
3943 iface
->ifname
, iface
->cross_connect_uplink
);
3948 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
3950 struct wpa_supplicant
*iface
;
3952 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
3953 if (!iface
->cross_connect_enabled
)
3955 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
3958 if (!iface
->cross_connect_in_use
)
3961 wpa_msg(iface
->parent
, MSG_INFO
,
3962 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
3963 iface
->ifname
, iface
->cross_connect_uplink
);
3964 iface
->cross_connect_in_use
= 0;
3969 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
3971 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
3972 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
3973 wpa_s
->cross_connect_disallowed
)
3974 wpas_p2p_disable_cross_connect(wpa_s
);
3976 wpas_p2p_enable_cross_connect(wpa_s
);
3977 if (!wpa_s
->ap_iface
)
3978 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3982 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
3984 wpas_p2p_disable_cross_connect(wpa_s
);
3985 if (!wpa_s
->ap_iface
)
3986 wpas_p2p_set_group_idle_timeout(wpa_s
);
3990 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
3992 struct wpa_supplicant
*iface
;
3994 if (!wpa_s
->global
->cross_connection
)
3997 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
4000 if (iface
->drv_flags
&
4001 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
4003 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
4006 wpa_s
->cross_connect_enabled
= 1;
4007 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
4008 sizeof(wpa_s
->cross_connect_uplink
));
4009 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
4010 "%s to %s whenever uplink is available",
4011 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4013 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
4014 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4015 iface
->cross_connect_disallowed
||
4016 iface
->wpa_state
!= WPA_COMPLETED
)
4019 wpa_s
->cross_connect_in_use
= 1;
4020 wpa_msg(wpa_s
->parent
, MSG_INFO
,
4021 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
4022 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4028 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
4030 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
4031 !wpa_s
->p2p_in_provisioning
)
4032 return 0; /* not P2P client operation */
4034 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
4036 if (wpa_s
!= wpa_s
->parent
)
4037 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
4039 if (wpa_s
->global
->p2p
)
4040 p2p_group_formation_failed(wpa_s
->global
->p2p
);
4042 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4043 wpa_s
->parent
, NULL
);
4045 wpas_group_formation_completed(wpa_s
, 0);
4050 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
4052 struct p2p_channels chan
;
4054 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
4057 os_memset(&chan
, 0, sizeof(chan
));
4058 if (wpas_p2p_setup_channels(wpa_s
, &chan
)) {
4059 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
4064 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
);
4068 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
4070 struct wpa_global
*global
= wpa_s
->global
;
4074 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
4076 if (wpa_s
->pending_interface_name
[0] &&
4077 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
4080 peer
= p2p_get_go_neg_peer(global
->p2p
);
4082 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
4083 MACSTR
, MAC2STR(peer
));
4084 p2p_unauthorize(global
->p2p
, peer
);
4087 wpas_p2p_stop_find(wpa_s
);
4089 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4090 if (wpa_s
== global
->p2p_group_formation
&&
4091 (wpa_s
->p2p_in_provisioning
||
4092 wpa_s
->parent
->pending_interface_type
==
4093 WPA_IF_P2P_CLIENT
)) {
4094 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
4095 "formation found - cancelling",
4098 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4099 wpa_s
->parent
, NULL
);
4100 wpas_p2p_group_delete(wpa_s
);
4106 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
4114 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
4116 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
4119 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
4120 "being available anymore");
4121 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_UNAVAILABLE
;
4122 wpas_p2p_group_delete(wpa_s
);
4126 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
4127 int freq_24
, int freq_5
, int freq_overall
)
4129 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4130 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4132 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
4136 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
4139 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4141 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4144 if (hwaddr_aton(addr
, peer
))
4147 return p2p_unauthorize(p2p
, peer
);